python中多行的正则表达式匹配

2024-06-09 16:26:47 发布

您现在位置: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

它正在失败。感谢您对此的任何建议!你知道吗


Tags: 答案代码用户reidtype场景result
0条回答
网友
1楼 · 发布于 2024-06-09 16:26:47

docs

Note that even in MULTILINE mode, re.match() will only match at the beginning of the string and not at the beginning of each line.

改用search

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

作为建议,如果有多个匹配值,请使用findall

result = re.findall("meg401(.*)",a,re.M)
网友
2楼 · 发布于 2024-06-09 16:26:47
result = re.findall("meg401(.*)",a,re.M)

使用re.findall而不是re.match。你知道吗

re.match匹配字符串的开头。你知道吗

请参见演示。你知道吗

https://regex101.com/r/tX2bH4/7#python

相关问题 更多 >