gpt4 book ai didi

查找首字母大写的连续单词的正则表达式

转载 作者:行者123 更新时间:2023-12-03 15:10:10 26 4
gpt4 key购买 nike

我正在寻找一个正则表达式,它可以在一个句子中识别出一个句子中的连续单词以大写字母开头。
如果我们以下面的文字为例:

The A-Z Group is a long-establishedmarket leader in the provision ofinformation for the global air cargocommunity, and also for the defenceand security sectors through BDECLimited, publishers of the BritishDefence Equipment Catalogue andBritish Defence Industry Directory.


我希望能够检索以下内容:

The A-Z Group

BDEC Limited Defence Equipment

Catalogue British Defence

IndustryDefence Industry


这甚至可以用正则表达式吗?
如果是这样,有人可以推荐一个吗?

最佳答案

(更新:我一开始误解了你的问题。)

一个简单的案例是

/([A-Z][\w-]*(\s+[A-Z][\w-]*)+)/

如果存在不同语言结构的特殊情况,则可能需要对其进行修改。
ruby-1.9.2-p0 > %Q{The A-Z Group is a long-established market leader in the provision of information for the global air cargo community, and also for the defence and security sectors through BDEC Limited, publishers of the British Defence Equipment Catalogue and British Defence Industry Directory.}.scan(/([A-Z][\w-]*(\s+[A-Z][\w-]*)+)/).map{|i| i.first} => ["The A-Z Group", "BDEC Limited", "British Defence Equipment Catalogue", "British Defence Industry Directory"]

关于查找首字母大写的连续单词的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4113051/

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