Reputation: 5494
I am using Python 2.6 and am getting [what I think is] unexpected output from re.sub()
>>> re.sub('[aeiou]', '-', 'the cat sat on the mat')
'th- c-t s-t -n th- m-t'
>>> re.sub('[aeiou]', '-', 'the cat sat on the mat', re.IGNORECASE)
'th- c-t sat on the mat'
If this output is what is expected, what is the logic behind it?
Upvotes: 3
Views: 1009
Reputation: 18841
In case you've upgraded since asking this question. If you are using Python 2.7+, you don't need to use re.compile
. You can call sub
and specify flags
with a named argument.
>>> import re
>>> re.sub('[aeiou]', '-', 'the cat sat on the mat', flags=re.IGNORECASE)
'th- c-t s-t -n th- m-t'
Reference: https://docs.python.org/2/library/re.html#re.sub
Upvotes: 0
Reputation: 2173
To pass flags you can use re.compile
expression = re.compile('[aeiou]', re.IGNORECASE)
expression.sub('-', 'the cat sat on the mat')
Upvotes: 4
Reputation: 35667
Yes, the fourth parameter is count, not flags. You're telling it to apply the pattern twice (re.IGNORECASE = 2).
Upvotes: 7