python - 如何在 Python 3 的 assertRegex 中表达多行正则表达式?

标签 python regex unit-testing python-3.x

我正在使用 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), and re.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 the re.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/

相关文章:

regex - 佩尔。使用 {x,y} 元字符时正则表达式不匹配

python - 为 Django 单元测试创​​建测试数据库

javascript - 测试对象是否具有多个属性

Python Rijndael 加密

javascript - 查找字符串中字符出现的次数

python - python中创建线程重写另一个结构体

java - 检查 Scanner.next() 获取的字符串是否包含数字

java - 使用 Mockito 模拟方法行为时以任何顺序匹配列表

python - 在 python 中更新字典的最快方法

python - 随机 Numpy 数组的逐元素种子