Regexp for multiple consecutive capitalized words

Discussion in 'Perl Misc' started by 100amp@gmail.com, Mar 12, 2009.

  1. Guest

    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?
    , Mar 12, 2009
    #1
    1. Advertising

  2. 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/
    A. Sinan Unur, Mar 12, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peng Yu
    Replies:
    0
    Views:
    187
    Peng Yu
    Jul 16, 2010
  2. Trans
    Replies:
    4
    Views:
    152
    David A. Black
    Nov 30, 2005
  3. Ian Neubert
    Replies:
    1
    Views:
    121
    Payal Patel
    Dec 8, 2010
  4. Trans
    Replies:
    9
    Views:
    127
    ara.t.howard
    Apr 4, 2008
  5. User
    Replies:
    5
    Views:
    459
    Tad McClellan
    Jun 13, 2004
Loading...

Share This Page