Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
505 views
in Technique[技术] by (71.8m points)

regex - Using more than one flag in python re.findall

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 
 eND', (re.I, re.DOTALL))

Error :

Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    x = re.findall(r'CAT.+?END','Cat 
 eND',(re.I,re.DOTALL))
  File "C:Python27lib
e.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
  File "C:Python27lib
e.py", line 243, in _compile
    p = sre_compile.compile(pattern, flags)
  File "C:Python27libsre_compile.py", line 500, in compile
    p = sre_parse.parse(p, flags)
  File "C:Python27libsre_parse.py", line 673, in parse
    p = _parse_sub(source, pattern, 0)
  File "C:Python27libsre_parse.py", line 308, in _parse_sub
    itemsappend(_parse(source, state))
  File "C:Python27libsre_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 ?

question from:https://stackoverflow.com/questions/30651271/using-more-than-one-flag-in-python-re-findall

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, but you have to OR them together:

x = re.findall(pattern=r'CAT.+?END', string='Cat 
 eND', flags=re.I | re.DOTALL)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...