简体   繁体   English

将正则表达式与多行字符串匹配

[英]Matching a Regex against a multiline string

I am trying to match a Regex against a multi-line string, but the match fails after the first line. 我正在尝试将正则表达式与多行字符串匹配,但是第一行之后匹配失败。

These expressions work as expected: 这些表达式按预期工作:

>>> import re
>>> r = re.compile("a")
>>> a = "a"
>>> r.match(a)
<_sre.SRE_Match object; span=(0, 1), match='a'>
>>> a = "a\n"
>>> r.match(a)
<_sre.SRE_Match object; span=(0, 1), match='a'>
>>>

Whilst this expression does not work: 尽管此表达式不起作用:

>>> a = "\na"
>>> r.match(a)
>>>

re.match was designed to match from the first character (the start) of the string. re.match旨在从字符串的第一个字符(开头)开始进行匹配。 In the first two examples, the match works fine because a is the first character. 在前两个示例中,匹配很好,因为a是第一个字符。 In the last example however, the match fails because \\n is the first character. 但是,在最后一个示例中,匹配失败,因为\\n是第一个字符。

You need to use re.search in this case to have Python search for the a : 在这种情况下,您需要使用re.search来让Python搜索a

>>> import re
>>> r = re.compile("a")
>>> a = "\na"
>>> r.search(a)
<_sre.SRE_Match object; span=(1, 2), match='a'>
>>>

Also, just a note: if you are working with multi-line strings, then you will need to set the dot-all flag to have . 另外,请注意:如果使用多行字符串,则需要将dot-all标志设置为have . match newlines. 匹配换行符。 This can be done with re.DOTALL . 这可以通过re.DOTALL完成。

Why doesnt match work? 为什么不match

match searches the pattern at the start of the string. match在字符串的开头搜索模式。

How to correct? 如何纠正?

use search instead 使用search代替

>>> import re
>>> pat=re.compile('a')
>>> pat.search('\na')
<_sre.SRE_Match object at 0x7faef636d440>
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM