2015-01-15 35 views
-2

我是一个新的python用户。编写一些代码以使用正则表达式匹配多行场景。但无法得到答案。任何人都可以帮助我,如果我失去了一些东西。python中多行代码的正则表达式匹配

我试着在pythex.org它,当我从代码

a = """ 
MEG  Type  EntityId Level PrimVlan CC Inter(ms) CC Priority CC EnaStatus 
---------- -------- -------- ----- -------- ------------ ----------- ------------ 
meg401  lsp  1  4  3  3.3   6   enable 

MEP ID  Type  EntityId Level Intf RMEP ID Direction Active Status 
---------- -------- -------- ----- ------- -------- --------- ------------- 
meg401  lsp  1  4  0/5  451  down  disable 
""" 

result = re.match("meg401(.*)",a,re.M) 

print result 

它是失败的尝试被匹配所需的两个lines.But。欣赏任何建议!

+3

要匹配什么呢? – Kasramvd

+0

我想你想使用're.search'而不是're.match'。 –

回答

4

docs

注意,即使在MULTILINE模式,re.match()将只匹配字符串的开始,而不是在每行的开头。

使用search代替

result = re.search("meg401(.*)",a,re.M) 

作为建议,你有超过1个匹配值使用findall

result = re.findall("meg401(.*)",a,re.M)