【发布时间】:2016-03-01 09:38:35
【问题描述】:
你能帮我用正则表达式找到单引号内的所有单引号吗?
IE
'sinead o'connor','don't don't','whatever'
感谢您的建议。
【问题讨论】:
标签: python regex single-quotes
你能帮我用正则表达式找到单引号内的所有单引号吗?
IE
'sinead o'connor','don't don't','whatever'
感谢您的建议。
【问题讨论】:
标签: python regex single-quotes
好像你的字符串是用逗号分隔的。
re.sub(r"\b'\b", "''", s)
或
(?<=[^,])'(?!,|$)
例子:
>>> import re
>>> s = "'sinead o'connor','don't don't','whatever'"
>>> re.sub(r"\b'\b", "''", s)
"'sinead o''connor','don''t don''t','whatever'"
>>>
【讨论】:
即使没有正则表达式,您也可以做到这一点:
>>> string = "'sinead o'connor','don't don't','whatever'"
>>> string = string.replace("'", "''")
"''sinead o''connor'',''don''t don''t'',''whatever''"
>>> string.strip("'")
"sinead o''connor'',''don''t don''t'',''whatever"
【讨论】:
【讨论】: