Reputation: 10256
I have this pattern:
>>> pat = r'(?:.*)?(name)|nombres?'
When I test:
>>> import re
>>> re.search('nombre', pat).group()
>>> 'nombre'
>>> re.search('name', pat).group()
>>> 'name'
But
>>> re.search('first_name', pat).group()
>>> *** AttributeError: 'NoneType' object has no attribute 'group'
Upvotes: 0
Views: 45
Reputation: 151
As already answered the arguments are swapped it should be:
re.search(pat, 'first_name').group()
I'd say also that you may want to check if the pattern has actually matched before trying to extract the group match:
result = re.search(pat, 'first_name')
if result:
print(result.group())
else:
print("not found")
Upvotes: 1
Reputation: 281012
You have the arguments in the wrong order. The pattern goes first.
Upvotes: 2