如何使用regex-python替换特定部分

如何使用regex-python替换特定部分,python,regex,Python,Regex,视图.py combineKeyword =""myKeyword", "Auto", "st3-throttling-cdb", "functional"" template_desc2 = "@Test(groups = { "Code-planned", "Code-not automated", "Code-st3reporter" } import re re.sub(r' @Test\(groups\s*=\s*\{([^\}]+)',combineKeyword,template_

视图.py

combineKeyword =""myKeyword", "Auto", "st3-throttling-cdb", "functional""
template_desc2 = "@Test(groups = { "Code-planned", "Code-not automated", "Code-st3reporter" }
import re
re.sub(r' @Test\(groups\s*=\s*\{([^\}]+)',combineKeyword,template_desc2)

这里我想替换“代码计划”、“代码非自动”、“代码-st3reporter”这些关键字,并将combineKeyword放在那里

您的示例有语法错误,我假设字符串用单引号括起来',内部字符串类似于“代码计划”

如果要替换所有字符串,无论它们是否以代码开头:

s = '@Test(groups = { "Code-planned", "Code-not automated", "Code-st3reporter" ,"different"}'

print re.sub(r'\"(.*?)\"','"my new string"',s)

@Test(groups = { "my new string", "my new string", "my new string" ,"my new string"}

清楚地显示您的示例输入和预期输出。还要添加到目前为止的正则表达式,这需要帮助才能完成。该代码中有一些语法错误。提示:如果您使用
表示字符串,则该字符串不能包含未转换的
值。输入是combineKeyword字符串,我想将其替换为“代码计划”、“代码非自动”、“代码-st3reporter”这些单词
s = '@Test(groups = { "Code-planned", "Code-not automated", "Code-st3reporter" ,"different"}'

print re.sub(r'\"(.*?)\"','"my new string"',s)

@Test(groups = { "my new string", "my new string", "my new string" ,"my new string"}