在 Python 中用正则表达式替换多个项目 [英] Replacing multiple items with regex in Python
问题描述
我有一个文本文件,其中包含我想要修改的某些字符串序列.例如,在下面的字符串中,我想用一个唯一的字符串替换 foo 和 bar(新字符串将基于最初匹配的内容,所以我不会事先知道).
I have a text file that contains certain string sequences that I want to modify. For example, in the following string I would like to replace foo and bar each with a unique string (The new string will be based on what originally matches, so I won't know it before hand).
Original: foo text text bar text
Replaced: fooNew text text bar_replaced text
我正在使用正则表达式根据实际文本中的分隔方式查找需要更改的组.如果我只是使用re.findAll(),在修改匹配的组后,我不再有字符串中单词的位置来重建字符串.
I am using regex to find the groups that I need to change based on how they are delimited in the actual text. If I just use re.findAll(), I no longer have the location of the words in the string to reconstruct the string after modifying the matched groups.
有没有办法在单独修改每个匹配项的同时保留字符串中单词的位置?
Is there a way to preserve the location of the words in the string while modifying each match separately?
推荐答案
选项 1
对于复杂的场景,我会推荐这个.这是带有 re.sub
和 lambda 回调的解决方案:
I would recommend this for complicated scenarios. Here's a solution with re.sub
and a lambda callback:
In [1]: re.sub('foo|bar', lambda x: 'fooNew' if x.group() == 'foo' else 'bar_replaced', text)
Out[1]: 'fooNew text text bar_replaced text'
<小时>
选项 2
简单得多,如果你有硬编码的字符串,可以用 str.replace
替换:
Much simpler, if you have hardcoded strings, the replacement is possible with str.replace
:
In [2]: text.replace('foo', 'fooNew').replace('bar', 'bar_replaced')
Out[2]: 'fooNew text text bar_replaced text'
这篇关于在 Python 中用正则表达式替换多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!