查找首字母大写的连续单词的正则表达式 [英] Regex that finds consecutive words with first letter capitalized

查看:58
本文介绍了查找首字母大写的连续单词的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个正则表达式,它可以在句子中识别出句子中的连续单词以大写字母开头.

I am looking for a regex that can identify in a sentence that consecutive words in a sentence start with capital letters.

如果我们以下面的文字为例:

If we take the text below as an example:

A-Z 集团是一家历史悠久的提供的市场领导者全球空运信息社区,也为国防和安全部门通过 BDEC有限公司,英国的出版商国防设备目录和英国国防工业名录.

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.

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

I want to be able to retrieve the following:

A-Z 组

BDEC 有限的防御设备

BDEC Limited Defence Equipment

英国国防目录

工业国防工业

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

Is this even possible with a regex? If so, can anyone suggest one?

推荐答案

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

(Update: I misunderstood your question at first.)

一个简单的例子

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

如果有不同语言结构的特殊情况,可能需要修改.

It may need to be modified if there are special cases of different language construct.

ruby-1.9.2-p0 >%Q{在为全球航空货运界以及国防和安全部门提供信息方面,AZ 集团是一家历史悠久的市场领导者,BDEC 有限公司是英国国防设备目录和英国国防工业目录的出版商.}.scan(/([AZ][\w-]*(\s+[AZ][\w-]*)+)/).map{|i|i.first}

<代码>=>[The A-Z Group"、BDEC Limited"、英国国防装备目录"、英国国防工业目录"]

这篇关于查找首字母大写的连续单词的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆