我有一个字符串,我想在其中将单引号转换为两个单引号。但不要转换字符串开头和结尾的单引号。
对于输入和输出:
输入->test is 'Are you there'
输出->test is 'Are you there'
输入->test is 'Are y'ou there'
输出->test is 'Are y''ou there'
我使用以下代码:
re.compile(r"(?<!()'(?!))").sub(string="test is 'Are you there'", repl="''")
但这会导致test is ''Are you there''
尝试使用Lookbehind & Lookahead
前任:
import re
d = ["test is 'Are you there'", "test is 'Are y'ou there'"]
for i in d:
print(re.sub(r"(?<=[A-Za-z])'(?=[A-Za-z])", "''", i))
输出:
test is 'Are you there'
test is 'Are y''ou there'