diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

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]}) | # (?!)))"; #}

Replies are listed 'Best First'.
Re: Reentrant re's / String::Approx in re's
by simonm (Vicar) on Nov 11, 2003 at 22:38 UTC
    It works on 5.6.1 as well, for what that's worth...
Re: Reentrant re's / String::Approx in re's
by Anonymous Monk on Nov 12, 2003 at 07:31 UTC
    Are there any perl versions where the re-engine is capable of working on more than one string at a time?
    No, the re-engine is not threaded.