4

I have a program like this :

import re

x='aaaaaaaa;aa;aaa;aaa;aaaaaaaaaa;'
x=re.sub(';','.',x, re.IGNORECASE)

print x

But the output is like this:

aaaaaaaa.aa.aaa;aaa;aaaaaaaaaa;

There are still some ; not replaced by a ., why ?

Using Python 2.6

1
  • 2
    Using "ignorecase" when replacing ; doesn't seem to make much sense. Commented May 13, 2012 at 10:53

1 Answer 1

7

Update - In Python 2.6 you can just do this:

>>> re.sub('(?i);','.',x)
'aaaaaaaa.aa.aaa.aaa.aaaaaaaaaa.'

For Python 2.7+ and 3.0+

Do this instead, the third parameter is actually the count(number of replacements to make) and re.IGNORECASE is simply an integer so it is using that as the count.

>>> re.sub(';','.',x, flags=re.IGNORECASE)
'aaaaaaaa.aa.aaa.aaa.aaaaaaaaaa.'

>>> re.IGNORECASE
2
Sign up to request clarification or add additional context in comments.

4 Comments

It gives me an error with python 2.6: TypeError: sub() got an unexpected keyword argument 'flags' (but it's work with the 2.7 version thank you ^^)
Which version of Python are you using? EDIT: Nvm read your comment
You can change your pattern to '(?i);' which does the same thing
(?i) came very handy as I need to be using python2.6 for the work I'm doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.