使用正则表达式python查找字符串中等号后的单引号

使用正则表达式python查找字符串中等号后的单引号,python,regex,Python,Regex,我有一个字符串列表,格式如下: value=“你好” 问题是,在某些情况下,等号后没有双引号(可能是单引号,也可能根本没有引号) 我试图在字符串中添加双引号 我在regex101.com上使用python模式尝试了以下正则表达式python代码: import re string1="value='hello''" string1=string1.replace("''",'"') regex = r'=[^\"]' subst = '=\"' # You can manually sp

我有一个字符串列表,格式如下:

value=“你好”

问题是,在某些情况下,等号后没有双引号(可能是单引号,也可能根本没有引号)

我试图在字符串中添加双引号

我在regex101.com上使用python模式尝试了以下正则表达式python代码:

import re

string1="value='hello''"
string1=string1.replace("''",'"')

regex = r'=[^\"]'

subst = '=\"'

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, string1, 0, re.MULTILINE)

print(result)

# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
我希望结果能打印出来

"hello"
但是我越来越

'hello"

您可以使用一种模式来查找包含在可选单引号中的单词,并将其替换为双引号中的单词:

print(re.sub(r"(?<==)'*(\w+)'*", r'"\1"', "value='hello''"))
print(re.sub(r"(?<==)'*(\w+)'*", r'"\1"', "value=hello"))

您可以使用一种模式来查找包含在可选单引号中的单词,并将其替换为双引号中的单词:

print(re.sub(r"(?<==)'*(\w+)'*", r'"\1"', "value='hello''"))
print(re.sub(r"(?<==)'*(\w+)'*", r'"\1"', "value=hello"))

为什么不删除单引号
<代码>“'hello”“”。替换(“”,“”)?我将根据模式r'\w*=\“*?\””从字符串中提取值。为什么不删除单引号
<代码>“'hello”“”。替换(“”,“”)?将根据模式r'\w*=\“*?\”从字符串中提取值。