简体   繁体   English

Python regex re.sub:某事

[英]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 . 没有被a取代. , why ? ,为什么?

Using Python 2.6 使用Python 2.6

Update - In Python 2.6 you can just do this: 更新 - 在Python 2.6中,您可以这样做:

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

For Python 2.7+ and 3.0+ 适用于Python 2.7+和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.IGNORECASE ,第三个参数实际上是count(要替换的数量), re.IGNORECASE只是一个整数,所以它使用它作为计数。

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

>>> re.IGNORECASE
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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