Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regexp for multiple consecutive capitalized words

Reply
Thread Tools

Regexp for multiple consecutive capitalized words

 
 
100amp@gmail.com
Guest
Posts: n/a
 
      03-12-2009
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?
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-12-2009
wrote in news:7d880468-1391-46f5-9522-6d244ca57b29
@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 <>
(remove .invalid and reverse each component for email address)

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
soap4r and capitalized wsdl:part names Ian Neubert Ruby 1 12-08-2010 08:09 PM
How to list all the python help topics that are capitalized? Peng Yu Python 0 07-16-2010 07:13 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
ClassMethods: Module creation hook / Capitalized method Trans Ruby 4 11-30-2005 06:09 PM
Grep Pattern, matching any two consecutive words having 3 to 8 chars each User Perl Misc 5 06-13-2004 12:14 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57