正则表达式查找不是以句子开头的以大写字母开头的单词

时间:2018-08-29 12:42:34

标签: python regex

我设法找到了以大写字母开头的单词,但无法找出正则表达式来过滤出从句子开头开始的单词。

每个句子以句号和空格结尾。

  • Test_string = This is a Test sentence. The sentence is Supposed to Ignore the Words at the beginning of the Sentence.

  • 所需的输出= ['Test', 'Supposed', 'Ignore', 'Words', 'Sentence']

我正在用Python编码。 如果有人可以帮我解决正则表达式,将感到高兴:)

2 个答案:

答案 0 :(得分:1)

您可以使用以下表达式:

(?<!^)(?<!\. )[A-Z][a-z]+

正则表达式演示here


import re
mystr="This is a Test sentence. The sentence is Supposed to Ignore the Words at the beginning of the Sentence."

print(re.findall(r'(?<!^)(?<!\. )[A-Z][a-z]+',mystr))

打印:

['Test', 'Supposed', 'Ignore', 'Words', 'Sentence']

答案 1 :(得分:0)

一个非常基本的选项。有关说明,请参见here

[^.]\s([A-Z]\w+)

import re
s = 'This is a Test sentence. The sentence is Supposed to Ignore the Words at the beginning of the Sentence, And others.'
re.findall(r'[^.]\s([A-Z]\w+)', s)

输出

['Test', 'Supposed', 'Ignore', 'Words', 'Sentence', 'And']