在 python 中使用正则表达式加字典或哈希映射动态替换句子中单词的所有开始和结束字母

Dynamically replace all starting and ending letters of words in a sentence by using regex plus a dictionary or Hash map in python

我正在寻找一种方法来创建一个动态替换句子中单词的所有首字母或开头字母的函数。我创建了一个替换首字母没问题的函数。

def replace_all_initial_letters(original, new, sentence):
    new_string = re.sub(r'\b'+original, new, sentence)
    return new_string

test_sentence = 'This was something that had to happen again'

print(replace_all_initial_letters('h', 'b', test_sentence))

Output: 'This was something that bad to bappen again'

不过,我希望能够使用字典或哈希图将多个选项输入到该函数中。例如使用以下内容:

initialLetterConversion = {
    'r': 'v',
    'h': 'b'
}

或者我认为可能有一种方法可以使用正则表达式分组来做到这一点。

我在实现结尾字母时也遇到了问题。我尝试了以下功能,但它不起作用

def replace_all_final_letters(original, new, sentence):
    new_string = re.sub(original+r'/s', new, sentence)
    return new_string

print(replace_all_final_letters('n', 'm', test_sentence))

Expected Output: 'This was something that had to happem agaim'

如有任何帮助,我们将不胜感激。

通过“简单”分组,您可以使用 lastindex 属性访问 匹配 。请注意,此类索引从 1 开始。 re.sub 接受回调作为第二个参数,以增加自定义替换的灵活性。这是一个用法示例:

import re


mapper = [
    {'regex': r'\b(w)', 'replace_with': 'W'},
    {'regex': r'\b(h)', 'replace_with': 'H'}]


regex = '|'.join(d['regex'] for d in mapper)


def replacer(match):
    return mapper[match.lastindex - 1]['replace_with'] # mapper is globally defined

text = 'This was something that had to happen again'

out = re.sub(regex, replacer, text)
print(out)
#This Was something that Had to Happen again

如果出于某种原因需要 re,请忽略此设置。这是简单的 Python,不需要任何导入。

转换映射是一个二元组列表。每个元组都有一个 fromto 值。 fromto 值不限于长度为 1 的字符串。

虽然映射是针对单词的 'ends',但此单个函数同时处理单词的开头和结尾,因此可能需要一些调整。

sentence = 'This was something that had to happen again'

def func(sentence, conv_map):
    words = sentence.split()
    for i, word in enumerate(words):
        for f, t in conv_map:
            if word.startswith(f):
                words[i] = t + word[len(f):]
                word = words[i]
            if word.endswith(f):
                words[i] = word[:-len(f)] + t
    return ' '.join(words)

print(func(sentence, [('h', 'b'), ('a', 'x'), ('s', 'y')]))

输出:

Thiy way yomething that bad to bappen xgain