我有一个很长的正则表达式。
vpa_pattern = '(VAP) ([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}): (.*)'
Run Code Online (Sandbox Code Playgroud)
我的匹配组代码如下:
class ReExpr:
def __init__(self):
self.string=None
def search(self,regexp,string):
self.string=string
self.rematch = re.search(regexp, self.string)
return bool(self.rematch)
def group(self,i):
return self.rematch.group(i)
m = ReExpr()
if m.search(vpa_pattern,line):
print m.group(1)
print m.group(2)
print m.group(3)
Run Code Online (Sandbox Code Playgroud)
我尝试通过以下方式使正则表达式模式变为多行,
vpa_pattern = '(VAP) \
([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}):\
(.*)'
Run Code Online (Sandbox Code Playgroud)
甚至我尝试过:
vpa_pattern = re.compile(('(VAP) \
([0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}:[0-9A-Fa-f]{2}):\
(.*)'))
Run Code Online (Sandbox Code Playgroud)
但是上述方法不起作用。对于每个组,在打开和关闭括号后我都有一个空格()。我猜想当我拆分为多行时它不会恢复。
看一下re.X标志。它允许注释,并忽略正则表达式中的空格。
a = re.compile(r"""\d + # the integral part
\. # the decimal point
\d * # some fractional digits""", re.X)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
373 次 |
| 最近记录: |