我试图用regex
从下面的文本中提取一个27个字符长的子字符串DpIJr_dR-DNu5kcR9RGmRprcnGU
text = '[[\"jewelry_designer\"]\n,[\"watch_store\"]\n,[\"jewelry_appraiser\"]\n,[\"leather_goods_store\"]\n]\n,null,\"DpIJr_dR-DNu5kcR9RGmRprcnGU\",null,null,null,[null]'
到目前为止,我隔离了被\"
包围的字符串,如下所示
pattern = '\\"(.*?)\\"'
output = re.findall(pattern, text)
### output => ['jewelry_designer', 'watch_store', 'jewelry_appraiser', 'leather_goods_store', 'DpIJr_dR-DNu5kcR9RGmRprcnGU']
我的下一步是在输出中添加一个长度约束,这样它只匹配27个字符长的子字符串
我试过\\"(.*?){27}\\"
或\\"(.*?{27})\\"
,但没有成功。我可以做[x for x in output if len(x) == 27]
,但那将是一个耻辱
试试这个:
Demo
首先将
\"
与\\\"
匹配,然后匹配并捕获您感兴趣的字符串[^\"]{27}
(除了重复引用27次以外的任何内容),然后再次将\"
与\\\"
匹配相关问题 更多 >
编程相关推荐