Parsing tags from a file using Regexp :: Grammars

I am trying to grab free tags from comments in a program using Perl and the Regexp::Grammars CPAN module.

 use strict; use v5.10; use YAML; my $s = q{ junk code; // here be tags #:tag1: junk code 2; // another one #:tag2: junk ...; }; my $rg = do { use Regexp::Grammars; qr{ <nocontext: > ^ .* <Tagger> .* $ <rule: Tagger> <[MATCH=single_tag]> + <token: single_tag> \#\:<tag>\: <token: tag> <matchline> \w+ }xms; }; if( $s =~ $rg ) { say Dump( \%/ ); } else { say 'no match'; } 

But the output of YAML shows that I only commit the last tag:

 --- Tagger: - tag: matchline: 5 

How can I match all tags from input?

And ... how can I get the correspondence of a tag line without including noisy context lines (removing the nocontext: directive), so that the final result is somewhat readable, i.e.

 --- Tagger: - tag: tag1 matchline: 3 - tag: tag2 matchline: 5 
+4
source share
1 answer

Found:

 my $rg = do { use Regexp::Grammars; qr{ <nocontext: > <Tagger> <rule: Tagger> <[MATCH=single_tag]>+ % (.*) <token: single_tag> <matchline> \#\:<tag>\: <token: tag> \w+ }xms; }; 

Which gives the following YAML:

 --- Tagger: - matchline: 3 tag: tag1 - matchline: 5 tag: tag2 
0
source

Source: https://habr.com/ru/post/1484477/


All Articles