简体   繁体   English

Python3 使用带有正则表达式的字典替换字符串

[英]Python3 Replace String Using Dict with Regex

I have an input field which a user can insert variables that are enclosed within a *, I am then using regex to grab the variables and I'm trying to look them up in the dict and then replace them in the input string.我有一个输入字段,用户可以在其中插入包含在 * 中的变量,然后我使用正则表达式来获取变量,我试图在字典中查找它们,然后在输入字符串中替换它们。 In the below code I have managed to write the regex that gets the variables and then matches it and creates a list with the values but I am unsure on how to use it to replace what's in the string.在下面的代码中,我设法编写了获取变量的正则表达式,然后匹配它并创建一个包含值的列表,但我不确定如何使用它来替换字符串中的内容。

variables = {
    '*FFullName*': 'Raz Smith',
    '*FFirstName*': 'Raz',
    '*FSurname*': 'Smith',
    '*Subject*': 'hello',
    '*Day*': '27',
}

input = '*Day* *Subject* to *FFirstName* *FSurname*'

#get all the *variables* and add to list
var_input = re.findall('(\*.*?\*)', input)

#match above list with dict
match = list(map(variables.__getitem__, var_input))

#how to replace these in input?

#expected outcome: input = '27 Hello to Raz Smith'

I got close by using the below code that found on here, however, it doesn't match when the variables in the input field have no space.我通过使用此处找到的以下代码接近,但是,当输入字段中的变量没有空格时,它不匹配。

#example of input not working correctly

input = '*Day**Subject*to*FFirstName**FSurname*'

pattern = re.compile(r'(?<!\*)(' + '|'.join(re.escape(key) for key in variables.keys()) + r')(?!\*)')
result = pattern.sub(lambda x: variables[x.group()], input)

You can use您可以使用

import re
 
variables = {
    '*FFullName*': 'Raz Smith',
    '*FFirstName*': 'Raz',
    '*FSurname*': 'Smith',
    '*Subject*': 'hello',
    '*Day*': '27',
}
 
text = '*Day* *Subject* to *FFirstName* *FSurname*'
var_input = re.sub(r'\*[^*]*\*', lambda x: variables.get(x.group(), x.group()), text)
print(var_input)
# => 27 hello to Raz Smith

See the Python demo请参阅Python 演示

You do not need to capture the whole matches, that is why ( and ) are removed now from the pattern.您不需要捕获整个匹配项,这就是现在从模式中删除()的原因。

The \*[^*]*\* pattern matches a * , then zero or more chars other than * and then a * . \*[^*]*\*模式匹配* ,然后匹配除*以外的零个或多个字符,然后匹配*

The whole match is passed to re.sub replacement argument via a lambda expression and the variables.get(x.group(), x.group()) fetches the corresponding value from the variables dictionary, or puts the match back if there is no item with the match value as key.整个匹配通过 lambda 表达式传递给re.sub替换参数,并且variables.get(x.group(), x.group())variables字典中获取相应的值,或者如果有则放回匹配没有以匹配值作为键的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM