some command [first line]\n
second line \n
yes can have multiple lines\n
\n
something else that I do not care about.
这是我到目前为止尝试过的:
>>> match = re.match(r"^(.+)\n((.*\n)*)\n",body,re.MULTILINE)
>>> match.groups()
('some command [first line]', 'second line \nyes can have multiple lines\n', 'yes can have multiple lines\n')
我正在寻找match.group(1)和match.group(2),并且对它们感到满意,但是我很困扰我得到了match.group(3),这不是我预期的(让我觉得我的正则表达式不正确)。
此外,我似乎无法正确使用命名模式..
match = re.match(r"^(.+)\n((?P<bd>.*\n)*)\n",body,re.MULTILINE)
>>> match.group(bd)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'bd' is not defined
我阅读了Google的Python正则表达式,但显然我还没有完全掌握。