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

Thanks .

All I wanted to know if the (.*?) matches 0 characters at the start of the string . I am aware that \d* matches 0 or more digits .

In this case , Both the captures match 0 characters at the start of the string and return nothing .

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

Replies are listed 'Best First'.
Re^3: Regex Minimal Quantifiers (updated)
by haukex (Archbishop) on May 24, 2017 at 15:56 UTC
    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".

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