繁体   English   中英

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

[英]Regex to find words starting with capital letters not at beginning of sentence

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

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

  • Test_string = This is a Test sentence. The sentence is Supposed to Ignore the Words at the beginning of the Sentence. 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 编码。 如果有人可以帮助我解决正则表达式,我会很高兴:)

您可以使用以下表达式:

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

正则表达式演示在这里


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']

一个非常基本的选择。 请参阅此处以获取解释。

[^.]\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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM