英文:
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 = """
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;
}
}
}
"""
rx_block = re.compile(r"""
^(?P<name>[A-Z_-]+)\s+
(?P<body>\{
(?:(?:[^{}]+)|(?2))+
\})
""", re.M | re.X)
rx_inner = re.compile(r"interface ge-0/0/0.0;")
for block in rx_block.finditer(data):
m = rx_inner.search(block.group('body'))
if m:
print(block.group('name'))
这将产生以下结果:
CPE-MGT
这里有两点需要注意:你实际上不需要使用正则表达式进行内部搜索,因为它是一个静态字符串,另外,可能应该使用解析器解决方案。你可以在 regex101.com 上查看该表达式的演示。
英文:
You could use two patterns and the regex
module which offers recursion:
import regex as re
data = """
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;
}
}
}
"""
rx_block = re.compile(r"""
^(?P<name>[A-Z_-]+)\s+
(?P<body>\{
(?:(?:[^{}]+)|(?2))+
\})
""", re.M | re.X)
rx_inner = re.compile(r"interface ge-0/0/0.0;")
for block in rx_block.finditer(data):
m = rx_inner.search(block.group('body'))
if m:
print(block.group('name'))
<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论