在 python re.findall 中使用多个标志 [英] Using more than one flag in python re.findall
本文介绍了在 python re.findall 中使用多个标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 re.findall
函数中使用多个标志.更具体地说,我想同时使用 IGNORECASE
和 DOTALL
标志.
I would like to use more than one flag with the re.findall
function. More specifically, I would like to use the IGNORECASE
and DOTALL
flags at the same time.
x = re.findall(r'CAT.+?END', 'Cat \n eND', (re.I, re.DOTALL))
错误:
Traceback (most recent call last):
File "<pyshell#78>", line 1, in <module>
x = re.findall(r'CAT.+?END','Cat \n eND',(re.I,re.DOTALL))
File "C:\Python27\lib\re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
File "C:\Python27\lib\re.py", line 243, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Python27\lib\sre_compile.py", line 500, in compile
p = sre_parse.parse(p, flags)
File "C:\Python27\lib\sre_parse.py", line 673, in parse
p = _parse_sub(source, pattern, 0)
File "C:\Python27\lib\sre_parse.py", line 308, in _parse_sub
itemsappend(_parse(source, state))
File "C:\Python27\lib\sre_parse.py", line 401, in _parse
if state.flags & SRE_FLAG_VERBOSE:
TypeError: unsupported operand type(s) for &: 'tuple' and 'int'
有没有办法使用多个标志?
Is there a way to use more than one flag ?
推荐答案
是的,但你必须将它们 OR 在一起:
Yes, but you have to OR them together:
x = re.findall(pattern=r'CAT.+?END', string='Cat \n eND', flags=re.I | re.DOTALL)
这篇关于在 python re.findall 中使用多个标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文