[英]Python regex only matches in single-line mode not multi-line mode
Why are there no regex matches when this is multiline, but it works on one line?为什么当这是多行时没有正则表达式匹配,但它只在一行上工作?
Python 3.8.6 | packaged by conda-forge | (default, Dec 26 2020, 05:05:16)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.20.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import re
In [2]: msg = r"""
...: (\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))
...: |
...: (ProgrammingError: inf can not be used with MySQL)
...: """
In [3]: err_text = 'ProgrammingError: inf can not be used with MySQL'
In [4]: re.search(msg, err_text, re.MULTILINE | re.VERBOSE)
But if I don't break it up into multiple lines and omit the re.MULTILINE | re.VERBOSE
但是如果我不把它分成多行并省略re.MULTILINE | re.VERBOSE
re.MULTILINE | re.VERBOSE
, it works re.MULTILINE | re.VERBOSE
,它有效
In [5]: msg2 = r"""(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))|(ProgrammingError: inf can not be used with MySQL)"""
In [6]: re.search(msg2, err_text)
Out[6]: <re.Match object; span=(0, 48), match='ProgrammingError: inf can not be used with MySQL'>
I've been trying to figure it out here https://regex101.com/r/tkju6f/1 but no luck.我一直试图在这里弄清楚 https://regex101.com/r/tkju6f/1但没有运气。
This is because the newlines are considered literally and not ignored.这是因为换行符被认为是字面意思而不是被忽略。 Try instead using comments:尝试使用注释:
msg = r'''(?#
)(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))(?#
)|(?#
)(ProgrammingError: inf can not be used with MySQL)(?#
)'''
The parts between (?#
and )
will be ignored. (?#
和)
之间的部分将被忽略。
Multiline mode is not what you think: it just means that ^
(resp. $
) is not meant to match the beginning (resp. ending) of the string , but the beginning (resp. ending) of the line . 多行模式不是你想的那样:它只是意味着^
(resp. $
) 不是要匹配string的开头(resp. ending),而是要匹配line的开头(resp. ending)。
Full execution:完全执行:
>>> import re
>>> msg = r'''(?#
... )(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))(?#
... )|(?#
... )(ProgrammingError: inf can not be used with MySQL)(?#
... )'''
>>> err_text = 'ProgrammingError: inf can not be used with MySQL'
>>> print(re.search(msg, err_text))
<re.Match object; span=(0, 48), match='ProgrammingError: inf can not be used with MySQL'>
Here you can find the fixed version of your regex101.在这里您可以找到 regex101 的固定版本。
EDIT : If you don't want to modify the regular expression but just make it more readable, just break the python lines like this:编辑:如果您不想修改正则表达式而只是使其更具可读性,只需像这样打破 python 行:
msg = r'''(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))''' + \
r'''|''' + \
r'''(ProgrammingError: inf can not be used with MySQL)'''
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.