in reply to Re: Return value from positive look behind
in thread Return value from positive look behind

@Choroba: Another unusual thing happens if I run the code 2x in a row. The first if{} works but the second does not. Does the regex somehow need to reset?<\p>

my $body_text="there are 5 minutes left in the program"; if ($body_text=~m/(\d{1,2})(?=\smins|\sminutes)/g) { print "1st try: " . $1 . "\n"; } if ($body_text=~m/(\d{1,2})(?=\smins|\sminutes)/g) { print "2nd try: " . $1 . "\n"; }

Replies are listed 'Best First'.
Re^3: Return value from positive look behind
by choroba (Cardinal) on Nov 11, 2020 at 17:17 UTC
    That's because of the /g, it remembers where the match happend last time and tries to match again after it.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^3: Return value from positive look behind
by GrandFather (Saint) on Nov 11, 2020 at 20:28 UTC

    To illustrate the effect noted in choroba's reply try the following:

    use strict; use warnings; my $body_text="5 minutes left. Wait 6 minutes before nagging again."; if ($body_text=~m/(\d{1,2})(?=\smins|\sminutes)/g) { print "1st try: " . $1 . "\n"; } if ($body_text=~m/(\d{1,2})(?=\smins|\sminutes)/g) { print "2nd try: " . $1 . "\n"; }

    Prints:

    1st try: 5 2nd try: 6
    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond