我正在使用 assertRegex() function来自 unittest
Python 3.4 中的库。它只需要两个相关参数:要匹配的文本和正则表达式。这似乎阻止我表达任何与正则表达式相关的常量,例如 re.MULTILINE .
如何构建或指定多行正则表达式并将其与 assertRegex()
一起使用?或者,是否有解决我的问题的相关方法?
最佳答案
参见 here在 Python 文档中:
(?iLmsux)
(One or more letters from the set 'i', 'L', 'm', 's', 'u', 'x'.) The group matches the empty string; the letters set the corresponding flags: re.I (ignore case),
re.L
(locale dependent),re.M
(multi-line),re.S
(dot matches all),re.U
(Unicode dependent), andre.X
(verbose), for the entire regular expression. (The flags are described in Module Contents.) This is useful if you wish to include the flags as part of the regular expression, instead of passing a flag argument to there.compile()
function.Note that the
(?x)
flag changes how the expression is parsed. It should be used first in the expression string, or after one or more whitespace characters. If there are non-whitespace characters before the flag, the results are undefined.
关于python - 如何在 Python 3 的 assertRegex 中表达多行正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29702481/