在文本字符串中查找Python的值

huangapple go评论81阅读模式
英文:

Python find value in text string

问题

我正在使用Python编写一个网络自动化脚本,遇到了一个我不知道如何解决的问题。我有以下输出:

CPE-MGT {
    instance-type virtual-router;
    interface ge-0/0/0.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}
DATA {
    instance-type virtual-router;
    interface ge-0/0/1.0;
}
MGMT_BK {
    instance-type virtual-router;
    interface ge-0/0/2.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}

我需要获取特定主键接口的名称所在的位置;也许如果我举个例子会更清楚。

我有一个正则表达式,它查找文本并发现ge-0/0/0.0现在在文本中;我不知道的是如何获取CPE-MGT。

cmd = net_connect.send_command(
        juniper_cmds["sh_routing_instance"])
    logger.debug(f'{ip_dict[config_dict["csv_ip_column"]][ips]}:设备路由实例 {cmd}')
    regex_routing_instance = re.compile(f'{interface[0]}')
    routing_instance = regex_routing_instance.findall(cmd)

希望这对你有帮助。

英文:

I am building a network automation script in python, and I got into a problem that I don't know how to resolve. I have the following output

CPE-MGT {
    instance-type virtual-router;
    interface ge-0/0/0.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}
DATA {
    instance-type virtual-router;
    interface ge-0/0/1.0;
}
MGMT_BK {
    instance-type virtual-router;
    interface ge-0/0/2.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}

I need to obtain the name of there I find the name of specific primary key interfaces; perhaps it will be clearer if I give an example.

I have a regex that looks into the text and finds that ge-0/0/0.0 is in the text now; what I don't know is how to obtain CPE-MGT.

cmd = net_connect.send_command(
        juniper_cmds["sh_routing_instance"])
    logger.debug(f'{ip_dict[config_dict["csv_ip_colum"]][ips]}:Device routing instances {cmd}')
    regex_routing_instance = re.compile(f'{interface[0]}')
    routing_instance = regex_routing_instance.findall(cmd)```

</details>


# 答案1
**得分**: 2

你可以使用两个模式和 `regex` 模块来实现递归:

```python
import regex as re

data = &quot;&quot;&quot;
CPE-MGT {
    instance-type virtual-router;
    interface ge-0/0/0.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}
DATA {
    instance-type virtual-router;
    interface ge-0/0/1.0;
}
MGMT_BK {
    instance-type virtual-router;
    interface ge-0/0/2.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}
&quot;&quot;&quot;

rx_block = re.compile(r&quot;&quot;&quot;
    ^(?P&lt;name&gt;[A-Z_-]+)\s+
    (?P&lt;body&gt;\{
        (?:(?:[^{}]+)|(?2))+
    \})
&quot;&quot;&quot;, re.M | re.X)

rx_inner = re.compile(r&quot;interface ge-0/0/0.0;&quot;)

for block in rx_block.finditer(data):
    m = rx_inner.search(block.group(&#39;body&#39;))
    if m:
        print(block.group(&#39;name&#39;))

这将产生以下结果:

CPE-MGT

这里有两点需要注意:你实际上不需要使用正则表达式进行内部搜索,因为它是一个静态字符串,另外,可能应该使用解析器解决方案。你可以在 regex101.com 上查看该表达式的演示

英文:

You could use two patterns and the regex module which offers recursion:

import regex as re

data = &quot;&quot;&quot;
CPE-MGT {
    instance-type virtual-router;
    interface ge-0/0/0.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}
DATA {
    instance-type virtual-router;
    interface ge-0/0/1.0;
}
MGMT_BK {
    instance-type virtual-router;
    interface ge-0/0/2.0;
    routing-options {
        static {
            route 192.168.253.115/32 next-hop 192.168.100.1;
            route 0.0.0.0/0 next-hop 192.168.100.1;
        }
    }
}
&quot;&quot;&quot;

rx_block = re.compile(r&quot;&quot;&quot;
	^(?P&lt;name&gt;[A-Z_-]+)\s+
	(?P&lt;body&gt;\{
		(?:(?:[^{}]+)|(?2))+
	\})
&quot;&quot;&quot;, re.M | re.X)

rx_inner = re.compile(r&quot;interface ge-0/0/0.0;&quot;)

for block in rx_block.finditer(data):
	m = rx_inner.search(block.group(&#39;body&#39;))
	if m:
		print(block.group(&#39;name&#39;))

<hr>

This would yield

CPE-MGT

Two notes here: you wouldn't really need a regular expression for the inner search as it is a static string and two - probably use a parser solution instead.
See a demo for the expression on regex101.com.

huangapple
  • 本文由 发表于 2023年3月3日 20:03:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定