简体   繁体   English

使用 re 在 python 中进行不区分大小写的替换

[英]case insensitive replacement in python using re

with this code使用此代码

import re

str = 'hi my name is Alice'
print(re.sub(str, 'hi', 'hello', re.IGNORECASE))

I got hellohello

I expect it would be hello my name is Alice我希望它会是hello my name is Alice

What's wrong here?这里有什么问题?

From pydocs , the signature of re.sub() ispydocs , re.sub() 的签名是

re.sub(pattern, repl, string, count=0, flags=0)

so your code should be所以你的代码应该是

print(re.sub('hi', 'hello', str, flags=re.IGNORECASE))

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

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