无法获得正确的正则表达式模式

Can't get regex patterns right

我制作了一个函数,根据字符位置用多个模式替换单个字符的多个实例。

我发现有两种方法可以做到这一点:

  1. 这个看起来很糟糕,但它确实有效:

def xSubstitution(target_string):

while target_string.casefold().find('x') != -1:

    x_finded = target_string.casefold().find('x')

    if (x_finded == 0 and target_string[1] == ' ') or (target_string[x_finded-1] == ' ' and 
       ((target_string[-1] == 'x' or 'X') or target_string[x_finded+1] == ' ')):

        target_string = target_string.replace(target_string[x_finded], 'ecks', 1)

    elif (target_string[x_finded+1] != ' '):

        target_string = target_string.replace(target_string[x_finded], 'z', 1)
    else:

        target_string = target_string.replace(target_string[x_finded], 'cks', 1)

return(target_string)
  1. 这个在技术上是可行的,但我就是无法正确使用正则表达式模式:

    重新导入

def multipleRegexSubstitutions(句子):

patterns = {(r'^[xX]\s'): 'ecks ', (r'[^\w]\s?[xX](?!\w)'): 'ecks',
            (r'[\w][xX]'): 'cks', (r'[\w][xX][\w]'): 'cks',
            (r'^[xX][\w]'): 'z',(r'\s[xX][\w]'): 'z'}

regexes = [
    re.compile(p)
    for p in patterns
]

for regex in regexes:
    for match in re.finditer(regex, sentence):
        match_location = sentence.casefold().find('x', match.start(), match.end())
        sentence = sentence.replace(sentence[match_location], patterns.get(regex.pattern), 1)
return sentence

据我了解,第二个函数中唯一的问题是正则表达式模式。有人可以帮助我吗?

编辑:对不起,我忘了告诉你正则表达式正在寻找字符串中不同的 x 字符,并在 'Z' 的中间或结尾替换单词开头的 X 'cks' 的一个词,如果它是一个单独的 'x' 字符替换为 'ecks'

我会为此使用以下一组替换:

string = re.sub(r"\b[Xx]\b", "ecks", string)
string = re.sub(r"\b[Xx](?!\s)", "Z", string)
string = re.sub(r"(?<=\w)[Xx](?=\w)", "cks", string)

这里,

(?!\s) 

只是断言正则表达式不匹配任何空白字符,

\b

编辑:最后一个正则表达式也会匹配单词开头的 xX。所以我们可以使用下面的,而不是,

(?<=\w)[xX](?=\w)

确保必须是一个字符\wbefore/afterxX.

你需要\b(单词边界)和\B(单词边界以外的位置):

Replace an X in the beggining of a word for a 'Z'

re.sub(r'\bX\B', 'Z', s, flags=re.I)

In the middle or end of a word for 'cks'

re.sub(r'\BX', 'cks', s, flags=re.I)

If it is a lone 'x' char replace with 'ecks'

re.sub(r'\bX\b', 'ecks', s, flags=re.I)