in reply to Re^2: Regex Minimal Quantifiers
in thread Regex Minimal Quantifiers

In the case of (.*?)(\d+) , I got confused how (.*?) matches 'I have ' instead of an empty string

The reason is that the regex engine always works from left to right, which is why the .*? begins matching at the beginning of the string - again, use re 'debug'; to see it in action. If you wanted to capture only the digit(s), you could write your regex as /(\d+)/, which has to skip everything that's not a digit at the beginning of the string for it to begin matching.

Update: If by "if the (.*?) matches 0 characters at the start of the string" you are referring to the /(.*?)(\d+)/ regex, then note that the overall regex still has to match, so .*? doesn't match zero characters in this case, as the regex engine again works to left from right and the .*? consumes the "I have " in order for the \d+ to match the "2".

Replies are listed 'Best First'.
Re^4: Regex Minimal Quantifiers (updated)
by pr33 (Scribe) on May 24, 2017 at 17:37 UTC

    Thanks for the explanation . Using re helps understand much better .