Python 替换所有匹配正则表达式的出现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38156956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:26:36  来源:igfitidea点击:

Replace all occurrences that match regular expression

pythonregex

提问by tots_o_tater

I have a regular expression that searches for a string that contains '.00.'or '.11.'as follows:

我有一个正则表达式,用于搜索包含'.00.''.11.'如下的字符串:

.*\.(00|11)\..*

What I would like to do is replace all occurrences that match the pattern with 'X00X'or 'X11X'. For example, the string '.00..0..11.'would result in 'X00X.0.X11X'.

我想要做的是用'X00X'or替换所有与模式匹配的事件'X11X'。例如,字符串'.00..0..11.'将导致'X00X.0.X11X'.

I was looking into the Python re.sub method and am unsure of how to do this effectively. The returned match object only matches on the first occurrence and therefore doesn't work well. Any advice? Should I just be using a string replace for this task? Thanks.

我正在研究 Python re.sub 方法,但不确定如何有效地做到这一点。返回的匹配对象仅在第一次出现时匹配,因此效果不佳。有什么建议吗?我应该为此任务使用字符串替换吗?谢谢。

回答by Tim Pietzcker

re.sub()(docs for Python 2and Python 3) does replace all matches it finds, but your use of .*may have caused the regex to match too much (even other occurences of .00.etc.). Simply do:

re.sub()Python 2Python 3 的文档)确实替换了它找到的所有匹配项,但是您的使用.*可能会导致正则表达式匹配太多(甚至是其他情况.00.等)。简单地做:

In [2]: re.sub(r"\.(00|11)\.", r"XX", ".00..0..11.")
Out[2]: 'X00X.0.X11X'

Note that patterns cannot overlap:

请注意,模式不能重叠:

In [3]: re.sub(r"\.(00|11)\.", r"XX", ".00.11.")
Out[3]: 'X00X11.'

回答by Siva Prasad Koka

You can try this as well,

你也可以试试这个

data = "otherway-of-try-b-pool"
data_after_regex = re.sub(r'(-[a-z]-pool)', "", data)

out: otherway-of-try (above regEx removed '-b-pool' part)