开发者

How to save matching order in %/?

Consider the following rule

<rule: str>
   ( <[token1]> | <[token2]> ) +

the output (%/) will be the same for the following inputs

input1: <token1> <token2> <token1>
input2: <token1> <token1> <token2>

This is because the 2nd is appended to the first one within a list named 'token1' but there is no hint on the order of matching with respect to other tokens.

Any idea how to get the matching order saved into %/ ?

p.s. I have problems compiling my scripts with this %/ magical variable. It always gives me an error on the line following the use of %/. I assume that the compiler considers it as a beginning of regex without terminating /... Pleas开发者_JAVA百科e let me know if I am using it correctly.


You do it by turning <str> into <[str]> so you endup with array in $/->{str} like this

#!/usr/bin/perl --
use strict; use warnings;
my $jp = do {
    use Regexp::Grammars;
qr{
# Keep the big stick handy, just in case...
    <debug:on>
    <debug:step>

<nocontext: >  # Switch on context substring retention

# Match this...
<[str]>+

<rule: str>
   ( <MATCH=token1> | <MATCH=token2> ) +

<rule: token1> \<token1\>
<rule: token2> \<token2\>


}ixs};

for my $str( 
    q{input1: <token1> <token2> <token1>},
    q{input2: <token1> <token1> <token2>},
) {
    print "#<<<<#  $str  \n\n";
    if( $str =~ $jp ){
        dd(\%/) ; #/
    } else {
        print "## fail to match \n";
    }
    print "\n#>>>>#\n\n";
}

sub dd {
    use Data::Dumper;
    print Data::Dumper->new([@_])->Useqq(1)->Indent(1)->Dump, "\n";
}
__END__
#<<<<#  input1: <token1> <token2> <token1>  

$VAR1 = {
  "str" => [
    "<token1>",
    "<token2>",
    "<token1>"
  ]
};


#>>>>#

#<<<<#  input2: <token1> <token1> <token2>  

$VAR1 = {
  "str" => [
    "<token1>",
    "<token1>",
    "<token2>"
  ]
};


#>>>>#
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜