python - 正则表达式匹配可能包含 ''' '' ''' 的单引号字符串
问题描述
输入字符串可以是:
- "his 'pet''s name is tom' and she is 2 years old"
- " '''' "
- " '' "
- "function('name', test, 'age')"
我想从这些输入中获取单引号字符串,这些输入甚至可能包含''
在单引号字符串中。
我尝试否定前瞻(?!')
来忽略''
while 匹配。
'.*?'(?!')
我期望输出
- 'pet''s name is tom'
- ''''
- 'name' and 'age'
解决方案
r"'(.+?)'"
获取单引号字符串
import re
tx = "his 'pet''s name is tom' and she is 2 years old"
print(re.findall(r"\'(.+?)\'",tx))
#output : ['pet', 's name is tom']
推荐阅读
- python - Python:删除重复项之间的列表中的部分
- java - Spring Data MongoDB GroupOperation 转换为 ObjectID
- git - Git认为分支不是相同的
- mysql - 如何根据不同的列在Mysql中连接两个表并汇总它们的列
- r - 使用 fix() 或 edit() 函数更改 R 中的另一个函数
- macos - Mac chrome 无法添加受信任的站点
- python - 如何在循环外打印项目?
- c# - 使用(DataView dv = dt.DefaultView) 块后下拉列表数据绑定不起作用
- servlets - 如何使 jcaptcha 与 Spring Session 一起使用?
- forms - Haskell - 命题逻辑