开发者

Regex that finds consecutive words with first letter capitalized

开发者 https://www.devze.com 2023-01-23 15:03 出处:网络
I am looking for a regex that can identify in a sentence that consecutive words in a sentence start with capital letters.

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:

The A-Z Group is a long-established 开发者_JAVA技巧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:

The A-Z Group

BDEC Limited Defence Equipment

Catalogue British Defence

IndustryDefence Industry

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


(Update: I misunderstood your question at first.)

A simple case is

/([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{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"]


hopefully this will do what you want, but apologies if I've misunderstood:

([A-Z][a-zA-Z0-9-]*[\s]{0,1}){2,}

The regex searches for two or more consecutive occurences of the following sequence: a capital letter followed by any amount of lowercase/uppercase/numerical/hyphen characters (alter this to any range of non-whitespace characters to suit your needs of course), followed by a whitespace character.

Edit: I know it's common sense, but just make sure that you set the regex search to be case sensitive, caught me out when I tested it :p

Edit: The above regex will, as 動靜能量 points out, match the single word THE because it doesn't enforce that at least the first two items must have a space between them. Corrected version:

([A-Z][a-zA-Z0-9-]*)([\s][A-Z][a-zA-Z0-9-]*)+


Start off by thinking in non-technical terms. What do you want? A "word" followed by one or more groups of "a word separator followed by a word"

Now you just need to define the pattern for a "word" and a "word separator", and then combine those into a complete pattern.

When you break it down like that, a complex regex is nothing more than a few very simple pattern groups.


$mystring = "the United States of America has many big cities like New York and Los Angeles, and others like Atlanta";

@phrases = $mystring =~ /[A-Z][\w'-]\*(?:\s+[A-Z][\w'-]\*)\*/g;

print "\n" . join(", ", @phrases) . "\n\n# phrases = " . scalar(@phrases) . "\n\n";

OUTPUT:

$ ./try_me.pl

United States, America, New York, Los Angeles, Atlanta

\# phrases = 5
0

精彩评论

暂无评论...
验证码 换一张
取 消