in reply to RE greediness

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11159717 use warnings; $_ = "a b=c"; /(a)(.*?)(?|b=(\w)|()\z)/ or die;; printf "\$2 is [%s]\n", defined $2 ? $2 : "undefined"; printf "\$3 is [%s]\n", defined $3 ? $3 : "undefined"; $_ = "a c=c"; /(a)(.*?)(?|b=(\w)|()\z)/ or die;; printf "\$2 is [%s]\n", defined $2 ? $2 : "undefined"; printf "\$3 is [%s]\n", defined $3 ? $3 : "undefined";

Outputs:

$2 is [ ] $3 is [c] $2 is [ c=c] $3 is []

Replies are listed 'Best First'.
Re^2: RE greediness
by Danny (Chaplain) on May 29, 2024 at 22:43 UTC
    Nice! I like your use of ?|. I similarly ended up using an OR-ed expression in the last part, but a bit different since the real strings had some other baggage.