Python regex re.sub: something

I have a program like this:

import re

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

print x

But the conclusion is as follows:

aaaaaaaa.aa.aaa;aaa;aaaaaaaaaa;

There is still ;no substitute for .why?

Using Python 2.6

+3
source share
1 answer

Update. In Python 2.6, you can simply 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 a counter (the number of changes to replace), and re.IGNORECASEis just an integer, so it is used as a counter.

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

>>> re.IGNORECASE
2
+6
source

All Articles