Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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,我试图从多行字符串中提取以下行: eth6.36 Link encap:Ethernet HWaddr A0:36:9F:5F:24:EE \r\n inet addr:36.36.36.10 Bcast:36.36.36.255 Mask:255.255.255.0\r\n inet6 addr: fe80::a236:9fff:fe5f:24ee/64 当我试图仅提取eth6.36链接压缩时,我得到一个错误 test = 'ifconfig

我试图从多行字符串中提取以下行:

eth6.36   Link encap:Ethernet  HWaddr A0:36:9F:5F:24:EE  \r\n          inet addr:36.36.36.10  Bcast:36.36.36.255  Mask:255.255.255.0\r\n          inet6 addr: fe80::a236:9fff:fe5f:24ee/64
当我试图仅提取
eth6.36链接压缩时,我得到一个错误

test = 'ifconfig eth6.36\r\neth6.36   Link encap:Ethernet  HWaddr A0:36:9F:5F:24:EE  \r\n          inet addr:36.36.36.10  Bcast:36.36.36.255  Mask:255.255.255.0\r\n          inet6 addr: fe80::a236:9fff:fe5f:24ee/64 Scope:Link\r\n          UP BROADCAST MULTICAST  MTU:9000  Metric:1\r\n          RX packets:0 errors:0 dropped:0 overruns:0 frame:0\r\n          TX packets:62 errors:0 dropped:0 overruns:0 carrier:0\r\n          collisions:0 txqueuelen:0 \r\n          RX bytes:0 (0.0 b)  TX bytes:7004 (6.8 KiB)\r\n\r\n'

match = re.match('(eth6.36\sLink encap:)', test)
print match.groups()
...
AttributeError: 'NoneType' object has no attribute 'groups'
有什么想法吗?

用with代替。
\s
还需要一个量词

>>> re.findall(r'(eth6.36\s+Link encap:)',test, re.M)
['eth6.36   Link encap:']
如果您确定只有一个结果,请使用
搜索
并删除分组括号:

从字符串的开头匹配。改为使用,因为它匹配字符串中的任何位置:

>>> match = re.search('(eth6.36\s+Link encap:)', test)
>>> print match.groups()
('eth6.36   Link encap:',)

此外,还必须指定多个空格字符匹配:
\s+
(注意+)。

如果要这样做,则正则表达式的格式中存在错误

import re
test = 'ifconfig eth6.36\r\neth6.36   Link encap:Ethernet  HWaddr A0:36:9F:5F:24:EE  \r\n          inet addr:36.36.36.10  Bcast:36.36.36.255  Mask:255.255.255.0\r\n          inet6 addr: fe80::a236:9fff:fe5f:24ee/64 Scope:Link\r\n          UP BROADCAST MULTICAST  MTU:9000  Metric:1\r\n          RX packets:0 errors:0 dropped:0 overruns:0 frame:0\r\n          TX packets:62 errors:0 dropped:0 overruns:0 carrier:0\r\n          collisions:0 txqueuelen:0 \r\n          RX bytes:0 (0.0 b)  TX bytes:7004 (6.8 KiB)\r\n\r\n'

match = re.search('(eth6\.36\s*Link encap:)', test)
print match.groups()
输出

('eth6.36   Link encap:',)

哪个错误?你能分享吗?@Giordano:
match
将是
None
,因为regexp不匹配任何内容。然后,下一行将导致
AttributeError
,因为
None
没有
groups
属性。我冒昧地在问题中添加了这一点。我注意到您在搜索正则表达式中删除了点,而上面的其他人没有。他们遗漏了什么吗?如果没有转义,这个点在这个字符串中被认为是元吗?它们没有遗漏任何东西。
替换一个字符。在他们的例子中,它与
本身匹配(它可以与其他任何东西匹配)。在我的例子中,它正被
完全取代
('eth6.36   Link encap:',)