Get consecutive capitalized words using regex

前端 未结 3 1418
孤街浪徒
孤街浪徒 2020-12-15 09:51

I am having trouble with my regex for capturing consecutive capitalized words. Here is what I want the regex to capture:

\"said Polly Pocket and the toys\" -         


        
3条回答
  •  一生所求
    2020-12-15 10:08

    $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
    

提交回复
热议问题