Danny has asked for the wisdom of the Perl Monks concerning the following question:
prints$_ = "a b=c"; /(a)(.*)(?:b=(\w))?/; printf "\$2 is [%s]\n", defined $2 ? $2 : "undefined"; printf "\$3 is [%s]\n", defined $3 ? $3 : "undefined";
I thought that using '(.*?)' would match up to 'b' then allow the optional '(?:b=(\w))?' to match, but this doesn't seem to be the case. Since they are both optional I guess it doesn't work. How can I consume everything up to a potential (?:b=(\w))? and still get (?:b=(\w))? to match?$2 is [ b=c] $3 is [undefined]
prints$_ = "a b=c"; /(a)(.*?)(?:b=(\w))?/; printf "\$2 is [%s]\n", defined $2 ? $2 : "undefined"; printf "\$3 is [%s]\n", defined $3 ? $3 : "undefined";
$2 is [] $3 is [undefined]
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: RE greediness
by ikegami (Patriarch) on May 29, 2024 at 19:35 UTC | |
by Danny (Chaplain) on May 29, 2024 at 22:20 UTC | |
Re: RE greediness
by tybalt89 (Monsignor) on May 29, 2024 at 18:56 UTC | |
by Danny (Chaplain) on May 29, 2024 at 22:43 UTC | |
Re: RE greediness
by GrandFather (Saint) on May 29, 2024 at 21:13 UTC | |
Re: RE greediness
by Anonymous Monk on May 29, 2024 at 19:11 UTC | |
by choroba (Cardinal) on May 29, 2024 at 19:25 UTC | |
by Anonymous Monk on May 29, 2024 at 20:46 UTC |