正则表达式:提取一个27长的字符子字符串,子字符串由“”

2024-09-29 23:31:37 发布

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

我试图用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],但那将是一个耻辱


Tags: store字符串textoutputnullwatchdesignerdr

热门问题