문제

I am trying to find a way to extract only upper case words (at least three consecutive upper characters, plus numbers) from quite a long string using perl.

Example:

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

Output:

"GOD EPISODE4"

For some reason I cannot come up with a sensible way to do this, any ideas? Thanks!

도움이 되었습니까?

해결책

Use character classes:

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

(You stated uppercase characters and numbers. You didn't specify where the number would be. You also didn't say whether or not I need to do something with the number.

Edit your question to include other requirements).

다른 팁

This will get you any upper case words that are over 3 characters and which may or may not have numbers 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 modify it to look for upper case characters after the numbers:

my @matches = ($str =~ /\b([A-Z]{3,}+[0-9]*[A-Z]*)\b/g);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top