Are there any perl versions where the re-engine is capable of working on more than one string at a time? I want to insert approximate matching sections into my expressions but String::Approx uses various subst and match expressions internally. Is it safe to invoke the re-engine during a match on another string?

Similarly - I noticed I had to put that ugly $ONCE variable in there to prevent the first approximate match from succeeding. I think I'll probably be able to solve this better if once I find an approximate match I then search forward for the best match, and either set the re's current position ahead or contrive to fail until the re's pos() is equal the previously noted node success point. This would require I invoke the re-engine more than once on the same string. Again - is this safe?

Update 1: this sample code "works" on 5.8.1, whether it works in a more general case... that's what I'm after.
Update 2: I replaced the code with a similar version which demonstrates amatch() at work, doesn't use $ONCE and attempts an exact match before trying amatch().

use String::Approx 'amatch'; use re 'eval'; use re 'debug'; $r = qr/((?:@{[ ramatch( 'APT' ) ]}\s)?\w+\d)$/; $apt = ( "5678 DELAWARE AVENUE AOT 123" =~ $r )[0]; $\ = "\n"; print "\$apt=$apt"; sub ramatch { my $match = shift; my $match_len = length $match; $match =~ s(\\)(\\\\)g; $match =~ s(')(\\')g; return "(?x:\Q$match\E | (?(?{scalar amatch('$match',substr(\$_,pos(),$match_len)) +}) (?s:@{[ q[.] x $match_len]}) | (?!)))"; }
# This version wasn't very nice so I replaced it and commented it out. #use String::Approx 'amatch'; #use re 'eval'; # #$r = qr/(@{[ ramatch( 'APT' ) ]}\s\w+\d)$/; #$apt = ( "5678 DELAWARE AVENUE APT 123" =~ $r )[0]; # #$\ = "\n"; #print "\$apt=$apt"; # #sub ramatch { # my $match = shift; # my $match_len = length $match; # # escape $match # our $ONCE = 0; # return # "(?x:(?(?{\$ONCE += amatch('$match',substr(\$_,pos(),$match_l +en)); # \$ONCE>1}) # (?s:@{[ q[.] x $match_len]}) | # (?!)))"; #}

In reply to Reentrant re's / String::Approx in re's by diotalevi

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.