我有一个包含多行的字符串。在其中的某些行中,有一个字符串XX:XX:XX:XX或XX:XX:XX:XX:XX:XX,其中X是0-9的a-f。我想返回与此模式匹配的字符串列表。
尽管我不确定如何返回与此正则表达式匹配的字符串列表,但我认为正则表达式应为“(\ w {2}:){3,4} \ w {2}”。
import re
a = "Some text ff:ab:05:41\n\
Some other text 1a:23:44:ff:55"
regex = "(\w{2}:){3,4}\w{2}"
print a
print re.findall(regex, a)
b = "Some text ff:ab:05:41"
c = "Some other text 1a:23:44:ff:55"
print b
print c
print re.search(regex, b).group()
print re.search(regex, c).group()
上面的代码返回
Some text ff:ab:05:41
Some other text 1a:23:44:ff:55
['05:', 'ff:']
Some text ff:ab:05:41
Some other text 1a:23:44:ff:55
ff:ab:05:41
1a:23:44:ff:55
所以看起来正则表达式工作正常,但是我不确定为什么findall()没有返回[“ ff:ab:05:41”,“ 1a:23:44:ff:55”]