python正则表达式在单引号中查找所有单引号

2024-06-26 01:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

你能帮我用正则表达式在单引号里找到所有的单引号吗?在

IE公司

'sinead o'connor','don't don't','whatever'

谢谢你的建议。在


Tags: 公司建议iewhateverdon单引号connorsinead
3条回答

试试这个:

'[\w|\s]*([']*)[\w|\s]*'

检查regex101.com

即使没有regex,也可以实现这一点:

>>> 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"

你的字符串好像是用逗号分开的。在

re.sub(r"\b'\b", "''", s)

或者

^{pr2}$

DEMO

示例:

^{3}$

相关问题 更多 >