Comparison of Perl samples, where we print all the relevant parts, but only the relevant parts (and not the rest)

I am looking for a solution for this in Perl.

Example: If my line is:

my $content = 'toto /app/blah titi\nhuh/app/ttt wew';

and my pattern: /app/somethingI want to get as output in an array: /app/blahand /app/ttt.

(which basically means grep -E -o '\/app\/[a-z]*')

I can not make it work! I tried:

my $content = 'toto /app/blah titi\nhuh/app/ttt wew';
$content =~ m/(\/app\/[a-z]*)/;
print "Group: $1 $2\n";

but it only prints: /app/blah(not /app/ttt)

and anyway, I don't know how to put the results in a table:

my (@table) = @_;

the table does not contain anything!

THX

+3
source share
2 answers

You want to use a modifier /gso that the regular expression returns all matches in a list:

@table = $content =~ m/(\/app\/[a-z]*)/g;
print "Group: @table\n";

, @_ . , , .

+5

bash, Perl , :

$ echo foo000bar000baz | perl -ne 'for(m/(foo)|(baz)/g){ if ($_ ne "") { print "$_\n"}}'            
foo
baz

$ echo 'foo
> 123
> bar
> 456
> baz ' | perl -ne 'for(m/(foo)|(baz)/g){ if ($_ ne "") { print "$_\n"}}'
foo
baz

( , Perl):

STDIN perl: , cat $file

  • m/Regular Expression within parenthesis indicating capture group/g
  • for loop
  • if "" ()
0

All Articles