Ci3
Ci3

Reputation: 4842

Regex and verbose in python 3

I'm reading Dive into Python 3 about regular expressions and specifically using re.VERBOSE. I tried searching for a string, but it always returns "None". For example:

import re
pattern = '''
testing
'''

print(re.search(pattern, 'test', re.VERBOSE))

I had thought that this should return something other than None because the pattern of characters "test" exists in "testing". I had also thought that if it had been something like:

pattern = '''
^testing$
'''

Then it makes sense that I would get a return value of None if I searched for the same string. However, regardless, I always seem to get a return value of None. What am I doing wrong?

Upvotes: 0

Views: 361

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122002

You have your patterns and text-to-search mixed up.

You are looking for testing in text test, and the latter is not nearly long enough. :-)

If you reversed the two (pattern test, text testing) things work:

>>> import re
>>> pattern = '''
... test
... '''
>>> print(re.search(pattern, 'testing', re.VERBOSE))
<_sre.SRE_Match object at 0x1062f4c60>

Upvotes: 4

Related Questions