How to extract uppercase words in a long string using perl

I'm trying to find a way to extract only uppercase words (at least three consecutive upper characters, plus numbers) from a fairly long string using perl.

Example:

"Hello world, thank GOD it Friday, I can watch EPISODE4" 

      

Output:

"GOD EPISODE4"

      

For some reason I can't think of a sane way to do this, any ideas? Thank you!

0


source to share


2 answers


Use character classes:

my @matches = ( $string =~ /\b[[:upper:]|[:digit:]]{3,}+\b/g );
say join " - ", @matches;

      



(You indicated capital letters and numbers. You did not indicate where the number will be. You also did not say if I need to do anything with the number.

Modify your question to include other requirements).

+1


source


This will give you any uppercase words that have more than 3 characters and may or may not have a number at the end:

my $str = "Hello world, thank GOD its Friday, I can watch EPISODE4"; 
my @matches = ($str =~ /\b([A-Z]{3,}+[0-9]*)\b/g);

      



You can change it to search for uppercase characters after numbers:

my @matches = ($str =~ /\b([A-Z]{3,}+[0-9]*[A-Z]*)\b/g);

      

0


source







All Articles