Regex 使用Python正则表达式查找问题和短语 我想用Python正则表达式找到每个问题短语,所以基本上我需要找到一个初始的词性,并把里面的所有东西都检测到问号,避免中间的其他部分。

Regex 使用Python正则表达式查找问题和短语 我想用Python正则表达式找到每个问题短语,所以基本上我需要找到一个初始的词性,并把里面的所有东西都检测到问号,避免中间的其他部分。,regex,python-3.x,Regex,Python 3.x,所以我带着密码来了: questionRegex = re.compile(r'[?.!][A-Za-z\s]*\?') 然后我使用这个正则表达式来查找文本中的问题: text = ''' Maybe the barista’s looking at me because she thinks I’m attractive. I am in my blue shirt. So she has stringy hair? Who am I to complain about stringy ha

所以我带着密码来了:

questionRegex = re.compile(r'[?.!][A-Za-z\s]*\?')
然后我使用这个正则表达式来查找文本中的问题:

text = '''
Maybe the barista’s looking at me because she thinks I’m attractive. I am in my blue shirt. So she has stringy hair? Who am I to complain about stringy hair? Who do I think I am? Cary Grant?

And now John was doing temp work at the law firm of Fleurstein and Kaplowitz to get himself righted again. He had a strong six-month plan: he would save some money to pay Rebecca’s parents back for the house and be able to take some time off to focus on his writing—on his painting. In a few months, he would be back on his feet, probably even engaged to someone new. Maybe even that barista. Yes, almost paradoxically, temp work provided John with the stability he craved.

This is shit. It is utter shit. What are you talking about? Are you serious about this?
'''
像这样:

process = questionRegex.findall(text)
但我得到的结果是:

。她有一头细密的头发

??我以为我是谁

。你在说什么

问题是这篇课文中有5个问题。这意味着此正则表达式无法捕捉问题:

我是谁会抱怨我的长发? 你是认真的吗?
我的代码有什么问题,为什么不能像其他问题一样抓住这两个问题?

我明白了为什么您的正则表达式模式无法返回所有结果

以下字符串:

我是谁会抱怨我的长发? 你是认真的吗? 事实上,任何下一个问句都是在空格字符之后

因此,您可以简单地使用\s,而不是指定一组[?!]

模式变为:

In [20]: pattern = re.compile(r'\s[A-Za-z\s]*\?')

In [21]: pattern.findall(text)
Out[21]:
[' So she has stringy hair?',
 ' Who am I to complain about stringy hair?',
 ' Who do I think I am?',
 ' Cary Grant?',
 ' What are you talking about?',
 ' Are you serious about this?']
您可以尝试以下方法:

(?<=[\?\.\!]\s)[^\?\n\.]+?\?
比赛:

她有一头细密的头发

我是谁会抱怨我的长发

我以为我是谁

卡里·格兰特

你在说什么

你是认真的吗