更新时间:2022-11-12 09:11:07
根据文档, re.match
永远不允许在 line 的开头进行搜索:
According to documentation, re.match
never allows searching at the beginning of a line:
请注意,即使在
MULTILINE
模式下,re.match()
也将仅在字符串的开头而不是每行的开头进行匹配.
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.
您需要使用 re.search
:
regexString = r"^\s*"+item.feature_key+":"
pq = re.compile(regexString, re.M)
if pq.search(data):
关于原始字符串( r"^ \ s +"
)的小注释:在这种情况下,它等效于"\ s +"
,因为没有 \ s
转义序列(例如 \ r
或 \ n
),因此,Python将其视为原始字符串文字.不过,始终使用Python中的原始字符串文字(以及其他语言中的相应符号)来声明正则表达式模式也更安全.
A small note on the raw string (r"^\s+"
): in this case, it is equivalent to "\s+"
because there is no \s
escape sequence (like \r
or \n
), thus, Python treats it as a raw string literal. Still, it is safer to always declare regex patterns with raw string literals in Python (and with corresponding notations in other languages, too).