for below perl code, if without ?, will print test, otherwise will print null.
i know it is due to greedy perl regexp matching that eat out test by previous .*, i also know there should be a way to force perl to match if can match, can anyone help me to figure it out or lead me to the right direction?
my $str="This is a testing string";
print $1 if $str=~/This.*(test)?.*$/;
Problem:-
You have a pattern with a greedy quantifier like *, +, ?, or {}, and you want to stop it from being greedy.
A classic case of this is the na�ve substitution to remove tags from HTML. Although it looks appealing,
s#<TT>.*</TT>##gsi, actually deletes everything from the first open TT tag through the last closing one
This would turn "Even <TT>vi</TT> can edit <TT>troff</TT> effectively." into "Even
effectively", completely changing the meaning of the sentence!
Solution
Replace the offending greedy quantifier with the corresponding non-greedy version. That is, change *, +, ?,
and {} into *?, +?, ??, and {}?, respectively.
perl -wle '
$str="This is a testing string";
print $1 if $str=~/This.*?(test).*$/;
' infile.txt
Right my mistake, I had incorrectly cited the source material you did not cite as the source of your post, as being the 2nd Edition of the Perl Cookbook. It is indeed the First Edition where you got that from. Thank you for the correction.