Python 正则表达式只匹配单行模式而不匹配多行模式
Python regex only matches in single-line mode not multi-line mode
为什么在多行时没有正则表达式匹配,但在一行上有效?
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)
但如果我不将其分成多行并省略 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'>
我一直想弄清楚这里 https://regex101.com/r/tkju6f/1 但没有运气。
(对于 this 公关)
这是因为换行符是按字面意思考虑的,不会被忽略。尝试使用评论:
msg = r'''(?#
)(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))(?#
)|(?#
)(ProgrammingError: inf can not be used with MySQL)(?#
)'''
(?#
和)
之间的部分将被忽略。
Multiline mode 不是你想的那样:它只是意味着 ^
(resp. $
)并不意味着匹配 [= 的开头(resp. 结尾) 31=]string,但是 line.
的开头(resp.ending)
完全执行:
>>> 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 你可以找到你的 regex101 的固定版本。
EDIT:如果您不想修改正则表达式,只是想使其更具可读性,只需像这样打破 python 行:
msg = r'''(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))''' + \
r'''|''' + \
r'''(ProgrammingError: inf can not be used with MySQL)'''
为什么在多行时没有正则表达式匹配,但在一行上有效?
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)
但如果我不将其分成多行并省略 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'>
我一直想弄清楚这里 https://regex101.com/r/tkju6f/1 但没有运气。
(对于 this 公关)
这是因为换行符是按字面意思考虑的,不会被忽略。尝试使用评论:
msg = r'''(?#
)(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))(?#
)|(?#
)(ProgrammingError: inf can not be used with MySQL)(?#
)'''
(?#
和)
之间的部分将被忽略。
Multiline mode 不是你想的那样:它只是意味着 ^
(resp. $
)并不意味着匹配 [= 的开头(resp. 结尾) 31=]string,但是 line.
完全执行:
>>> 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 你可以找到你的 regex101 的固定版本。
EDIT:如果您不想修改正则表达式,只是想使其更具可读性,只需像这样打破 python 行:
msg = r'''(\(1054, "Unknown column 'inf(e0)?' in 'field list'"\))''' + \
r'''|''' + \
r'''(ProgrammingError: inf can not be used with MySQL)'''