tags:

views:

214

answers:

2

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?

+6  A: 

Yes, the fourth parameter is count, not flags. You're telling it to apply the pattern twice (re.IGNORECASE = 2).

roe
Ah yes. Mixing up parameters. Thank you.
kjfletch
+3  A: 

To pass flags you can use re.compile

expression = re.compile('[aeiou]', re.IGNORECASE)
expression.sub('-', 'the cat sat on the mat')
ymv