Hi everyone, I have a question related to regular expression and below are 2 code snippets. Can you please advise why they give diffferent output? What is wrong with code snippet 2 ?
Code Snippet 1
#!/usr/bin/perl use re qw(eval); my $pattern1; my $str1 = "RS"; $pattern1 = qr{(?<P_ROOTCODE>.*)(?{ push(@rc1, ${^MATCH}) })\K(?{ $p_r +ootcode = "@rc1" })(?<DAY1>[0-9])(?{ push(@rc2, ${^MATCH}) })(?<P_MON +_CODE>[A-Z])(?{ push(@rc3, ${^MATCH}) })$}; $str1 =~ m/$pattern1/; print "Value of p_rootcode in Pattern 1 is : $p_rootcode\n";
Code Snippet 2
#!/usr/bin/perl use re qw(eval); my $pattern2; my $str2 = "RS"; $pattern2 = qr{(?<P_ROOTCODE>.*)(?{ push(@rc1, ${^MATCH}) })\K(?{ $p_r +ootcode = "@rc1" })(?<DAY1>[0-9])(?{ push(@rc2, ${^MATCH}) })(?<P_MON +_CODE>[A-Z])(?{ push(@rc3, ${^MATCH}) })(?<P_NEW_MON_CODE>[A-Z])(?{ p +ush(@rc4, ${^MATCH}) })$}; $str2 =~ m/$pattern2/; print "Value of p_rootcode in Pattern 2 is : $p_rootcode\n";
In reply to Question on Regular Expression by sjain
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |