in reply to Question on Regular Expression

In the second snippet, you are matching against $pattern1, but you only defined $pattern2. Changing it to $pattern2 doesn't change the output, though.

Have you tried adding debug to the use re line? If I understand the documentation, you can remove eval from there, as it's useless for qr{}.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: Question on Regular Expression
by Anonymous Monk on Dec 27, 2014 at 08:15 UTC

    If I understand the documentation, you can remove eval from there, as it's useless for qr{}.

    Well, as far as I can tell, the code the OP posted doesn't make use of the eval feature at all , the code section is a literal

    literal

    variable aka "dynamic" aka it throws error :)

    $ perl -E " $_ = 123; $f = '(?{ warn pos })'; m/\d$f/; " Eval-group not allowed at runtime, use re 'eval' in regex m/\d(?{ warn + pos })/ at -e line 1. $ perl -E " $_ = 123; $f = '(?{ warn pos })'; $g = qr/\d$f/; m/$g/" Eval-group not allowed at runtime, use re 'eval' in regex m/\d(?{ warn + pos })/ at -e line 1.

    I believe whatever the OPs real code, he got this error message so he added the "solution"

    I also believe there is no need for any of this stuff :) if all the OP is doing is building a data structure, simply match in a loop and use %+ or use Regexp::Grammars

      Can you please let me know how to make use of %+ so to match in a loop even if the submatch in a pattern string fails?

        Can you please let me know how to make use of %+ so to match in a loop even if the submatch in a pattern string fails?

        um, \d always fails :)

        #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my $cool = qr/ (?<key> \s* (?<key2>\w+) \s* (?<keyQ>\w+) \s* (?<NEVER>\d*) ) /x; my $beans = 'castor cocoa coffee pinto navy Mayocoba'; while( $beans =~ m{$cool}g ){ dd( \%+ ); } __END__ { # tied Tie::Hash::NamedCapture key => "castor cocoa ", key2 => "castor", keyQ => "cocoa", NEVER => "", } { # tied Tie::Hash::NamedCapture key => "coffee pinto ", key2 => "coffee", keyQ => "pinto", NEVER => "", } { # tied Tie::Hash::NamedCapture key => "navy Mayocoba", key2 => "navy", keyQ => "Mayocoba", NEVER => "", }
Re^2: Question on Regular Expression
by sjain (Initiate) on Dec 27, 2014 at 12:10 UTC

    As you pointed out, There was a typo in code snippet 2. I have changed $pattern1 with $pattern2 in code snippet 2. But I am getting different out. Is it because I am using version 5.10 ?