Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The note is interesting: they are highlighting this difference between ECMA script REs and Perl REs.

The RE (x+)? is very similar to (x*), except that the latter will always match (and, therefore, never have the value from a previous match if it is in an enclosing repeating group. This is similar to the requirement in Note 3: "Step 4 of the RepeatMatcher clears Atom's captures each time Atom is repeated." Because it always matches it always has a value from the last repeat of the outer repeating group, as if it was reset for each repeat, except that the value is '' instead of undef in the case that x did not match. This is an easy transformation.

I appreciate that you don't want to change the RE but you say you are parsing it, so perhaps you can make some systematic transformations.

Consider:

use strict; use warnings; use Data::Dumper::Concise; my $string = "aacbbbcac"; my $re = '((a+)?(b+)?(c))*'; # transform '(x+)?' to '(x*)' assuming 'x' is monolithic $re =~ s/\Q+)?/*)/g; print "re = $re\n"; my $re1 = qr/$re/; if ($string =~ $re1) { my @something; foreach (0..$#-) { if(defined($-[$_])) { my $substring = substr($string, $-[$_], $+[$_] - $-[$_]); # ${$_} also works, except where $_ = 0 no strict 'refs'; print "\$substring = $substring = ${$_}\n"; # transform '' to undef $substring = undef if($substring eq ''); # assert: $substring is now as specified by # Standard ECMA-262, 5.1 Edition / June 2011 # Section 15.10.2.5 Note 3 printf "Group %d: <%s>\n", $_, $substring // ''; $something[$_] = $substring; } } print Dumper(\@something); }

Produces

re = ((a*)(b*)(c))* $substring = aacbbbcac = test.pl Group 0: <aacbbbcac> $substring = ac = ac Group 1: <ac> $substring = a = a Group 2: <a> $substring = = Group 3: <> $substring = c = c Group 4: <c> [ "aacbbbcac", "ac", "a", undef, "c" ]

In reply to Re^7: Explain a regexp matched group result by ig
in thread Explain a regexp matched group result by jdd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-18 04:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found