Regexp for multiple consecutive capitalized words

1

100amp

I'm trying to come up with a Perl regexp to capture multiple
capitalized words in a row.

For example, in the sentence "I love New York City in the springtime."

I want to capture "New York City", "New York", and "York City".

I've been playing around with this for awhile with little luck. Any
ideas?
 
A

A. Sinan Unur

(e-mail address removed) wrote in @h20g2000yqn.googlegroups.com:
I'm trying to come up with a Perl regexp to capture multiple
capitalized words in a row.

For example, in the sentence "I love New York City in the springtime."

I want to capture "New York City", "New York", and "York City".

I've been playing around with this for awhile with little luck. Any
ideas?

Here is one part of the task:

#!/usr/bin/perl

use strict;
use warnings;

my $text = <<EOT;
I love New York City in the springtime. The United Nations
is headquartered in New York City but the North Atlantic Treaty
Organization is headquartered in Brussels.
EOT

my $pat = '(?:[[:upper:]][[:alpha:]]+)';

my @matches = ( $text =~ /\s(${pat}(?:\s+${pat})+)/g );

for ( @matches ) {
s/\s+/ /g;
print $_, "\n";
}

__END__

C:\DOCUME~1\asu1\LOCALS~1\Temp> t
New York City
The United Nations
New York City
North Atlantic Treaty Organization


--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top