3
我在使用正则表达式搜索多行模式时遇到了一些麻烦。下面是示例多行字符串:多行正则表达式
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
我通过Python Regular Expressions from Google去了,但很明显,我还没有得到完整的图片呢。