2015-01-15 29 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)