Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在python中使用正则表达式匹配一个换行符,后跟一个字符串_Python_Regex - Fatal编程技术网

我想在python中使用正则表达式匹配一个换行符,后跟一个字符串

我想在python中使用正则表达式匹配一个换行符,后跟一个字符串,python,regex,Python,Regex,上面是一个文件中的snipet。从上面我只想通过匹配下面的两行来收集ip地址。 入境地址: IP地址:10.3.10.46 或 管理地址: IP地址:10.3.10.46 我编写的正则表达式如下所示,它不起作用,我无法解决如何在正则表达式中的“address(es):”之后显示新行 Device ID: xyz Entry address(es): IP address: 10.3.10.46 Platform: WS-x, Capabilities: Switch IGMP Inter

上面是一个文件中的snipet。从上面我只想通过匹配下面的两行来收集ip地址。 入境地址: IP地址:10.3.10.46 或 管理地址: IP地址:10.3.10.46

我编写的正则表达式如下所示,它不起作用,我无法解决如何在正则表达式中的“address(es):”之后显示新行

Device ID: xyz
Entry address(es): 
 IP address: 10.3.10.46
Platform: WS-x,  Capabilities: Switch IGMP 
Interface: GigabitEthernet9/33,  Port ID (outgoing port): GigabitEthernet0/2
Holdtime : 177 sec
Management address(es): 
 IP address: 10.3.10.46

请帮忙。谢谢。

您需要使用
re.search
而不是
re.match
re.match
用于从字符串开始进行匹配:

f = open(fileName)
for line in f:
    matchObj1 = re.match(r'Entry address\(es\):\s+IP address: ([0-9.]+)', line)
    if matchObj1:
    print "IP Address = ", matchObj1.group(1)

    matchObj2 = re.match(r'Entry address\(es\):\s+IP address: ([0-9.]+)', line)
    if matchObj2:
    print "IP Address = ", matchObj2.group(1)
:

re.match()
仅在字符串开头检查匹配项, 而
re.search()


您需要使用
re.search
而不是
re.match
re.match
用于从字符串开始进行匹配:

f = open(fileName)
for line in f:
    matchObj1 = re.match(r'Entry address\(es\):\s+IP address: ([0-9.]+)', line)
    if matchObj1:
    print "IP Address = ", matchObj1.group(1)

    matchObj2 = re.match(r'Entry address\(es\):\s+IP address: ([0-9.]+)', line)
    if matchObj2:
    print "IP Address = ", matchObj2.group(1)
:

re.match()
仅在字符串开头检查匹配项, 而
re.search()


您不需要为此使用正则表达式。您的输入看起来像配置文件或程序的输出,并且易于解析

>>> s = 'Device ID: xyz\nEntry address(es): \n      IP address: 10.3.10.46\n    Platform: WS-x,  Capabilities: Switch IGMP \n    Interface: GigabitEthernet9/33,  Port ID (outgoing port): GigabitEthernet0/2\n    Holdtime : 177 sec'
>>> re.search(r'Entry address\(es\):\s+IP address: ([0-9.]+)', s).group(1)
'10.3.10.46'

您不需要为此使用正则表达式。您的输入看起来像配置文件或程序的输出,并且易于解析

>>> s = 'Device ID: xyz\nEntry address(es): \n      IP address: 10.3.10.46\n    Platform: WS-x,  Capabilities: Switch IGMP \n    Interface: GigabitEthernet9/33,  Port ID (outgoing port): GigabitEthernet0/2\n    Holdtime : 177 sec'
>>> re.search(r'Entry address\(es\):\s+IP address: ([0-9.]+)', s).group(1)
'10.3.10.46'

谢谢你的回复,但当我有“入口地址:”后接IP地址和“管理地址:”后接同样格式的IP地址时,问题就来了。因此,我要寻找的是,使用正则表达式匹配由“\n”分隔的两行。这样做仍然不需要正则表达式(请查看我编辑的答案)。感谢您的答复,但当我有“Entry address(es):”后跟IP address和“Management address(es):”时会出现问题然后是同样格式的IP地址。因此,我要寻找的是,使用由“\n”分隔的正则表达式匹配这两行代码,您仍然不需要正则表达式(请看我编辑的答案)。