woggioni
woggioni

Reputation: 1432

strange behaviour of regexp

I have this str list

l=['Cu_3', 'P_2', 'O_8']

and this two regexp

import re
sub = re.compile('_([0-9])')    
sub2 = re.compile('._([0-9])')

sub doesn't match any elemetn of the list, while sub2 matches the last two but not the first one.. I am probably missing something but I expected to capture the numbers after the underscore with both of them. What's the problem here?

Upvotes: 0

Views: 54

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798706

The problem is that you're using the match() method instead of the search() method, so your regexs are anchored at the start of the string.

Upvotes: 4

Related Questions