用相同单词的大写和小写搜索和替换单词

时间:2018-09-25 12:33:13

标签: python python-3.x replace

此脚本的要点是替换单词的多个字符串,即使该单词以小写字母或大写字母开头。

代码示例:

import re
from re import sub

def word_replace(text, replace_dict):
    rc = re.compile(r"[A-Za-z_]\w*")
def translate(match):
    word = match.group(0) 
    return replace_dict.get(word, word)
return rc.sub(translate, text)

old_text = """Bob: say why don't you play ball
jeff: i have no idea
bob: well maybe you should """

replace_dict = {
"Bob" : 'bob baller',
"debug" : "fix",
'ship': 'boat'
 }

我得到的是:

bob baller: say why don't you play ball
jeff: i have no idea
bob: well maybe you should 

我想从文本中删除的是“鲍勃”和“鲍勃”,然后都用鲍勃·鲍尔代替。

为了进一步澄清这个问题,我要尝试的是替换单词'bob'(或replace_dict中的任何单词)(如果是大写或小写)。

2 个答案:

答案 0 :(得分:1)

使用其他类似的参数编译您的正则表达式

re.compile(“您的正则表达式在这里”,re.IGNORECASE)

编辑1:

好的,事实证明,由于双引号和单引号用法不一致,因此您的replace_dict格式不正确。 这是工作代码和预期输出:

bob_baller.py

import re

def word_replace(text, replace_dict):
    rc = re.compile(r"[A-Za-z_]\w*")

    def translate(match):
        word = match.group(0).lower()
        print(word)
        return replace_dict.get(word, word)

    return rc.sub(translate, text)

old_text = """Bob: say why don't you play ball
jeff: i have no idea
bob: well maybe you should """

replace_dict = {
    "bob" : "bob baller",    # Everything is double quoted
    "debug" : "fix",
    "ship": "boat"
 }

output = word_replace(old_text, replace_dict)
print(output)

$ python bob_baller.py
bob baller: say why don't you play ball
jeff: i have no idea
bob baller: well maybe you should 

答案 1 :(得分:0)

您可以将replace_dict键转换为小写字母,然后匹配两个单词并进行替换。就像鲍勃和鲍勃的比赛一样。