Python regex compile (with re.VERBOSE) not working
Asked Answered
A

1

9

I'm trying to put comments in when compiling a regex but when using the re.VERBOSE flag I get no matchresult anymore.

(using Python 3.3.0)

Before:

regex = re.compile(r"Duke wann", re.IGNORECASE)
print(regex.search("He is called: Duke WAnn.").group())

Output: Duke WAnn

After:

regex = re.compile(r'''
Duke # First name 
Wann #Last Name
''', re.VERBOSE | re.IGNORECASE)

print(regex.search("He is called: Duke WAnn.").group())

Output:

AttributeError: 'NoneType' object has no attribute 'group'
Aara answered 7/12, 2012 at 10:58 Comment(2)
Actually that's the wrong syntax for a raw multiline string: r'''this is wrong''' . The right syntax must use r with double-quotes: r"""this is right""". See How to correctly write a raw multiline string in Python?Verboten
@Verboten No, they work exactly the same.Shavers
S
13

Whitespaces are ignored (ie, your expression is effectively DukeWann), so you need to make sure there's a space there:

regex = re.compile(r'''
Duke[ ] # First name followed by a space
Wann #Last Name
''', re.VERBOSE | re.IGNORECASE)

See http://docs.python.org/2/library/re.html#re.VERBOSE

Steersman answered 7/12, 2012 at 11:1 Comment(5)
Does this necessitate that all literal spaces be substituted with [ ] in the regex? This seems to be the case as far as I can tell.Swan
@Brad yes. They need to be explicitly stated as per one of the ways the docs mentionSteersman
Actually that's the wrong syntax for a raw multiline string: r'''this is wrong''' . The right syntax must use r with double-quotes: r"""this is right""". See How to correctly write a raw multiline string in Python?Verboten
@Verboten they're the same thing as far as the grammar is concerned so while one could argue using quotes is more PEP8 like I'm not sure exactly why you're asserting one is correct and the other isn't...?Steersman
@BradSolomon You can also escape the space (Duke\ ), use a hex escape (\x20)/oct escape (\040) or even something more weird like [^\S\r\n\t\f\v] (only in ASCII mode).Shavers

© 2022 - 2024 — McMap. All rights reserved.