简体   繁体   中英

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 output is like this:

aaaaaaaa.aa.aaa;aaa;aaaaaaaaaa;

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

Using Python 2.6

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM