gpt4 book ai didi

Python 的正则表达式星号量词没有按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:48 26 4
gpt4 key购买 nike

我正在尝试使用正则表达式来仅选择引号内的词组。

示例。

输入:

this is 'a sentence' with less 'than twenty words'

输出:

['a sentence', 'than twenty words']

我使用的正则表达式是:

'\'[\w]+[ ]+[[\w]+[ ]+]*[\w]+\''

但它只是返回“超过二十个字”。事实上,它只返回带有两个空格的字符串。

最佳答案

试试这个:

import re
re.findall(r"\'(\s*\w+\s+\w[\s\w]*)\'", input_string)

Demo

关于Python 的正则表达式星号量词没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com