in reply to Re: Non-greedy substitution
in thread Non-greedy substitution

If .+? were to match any less, $ wouldn't match.

I'm sorry, but I don't understand why .+? doesn't match 2 characters at position 5 - the match has to be tied to the end of the string...doesn't it?

Replies are listed 'Best First'.
Re^3: Non-greedy substitution
by ikegami (Patriarch) on Nov 15, 2024 at 19:55 UTC

    There can't be gaps in what matches. «.+?» must start matching where «,» left off. I added a "full" trace to my post.

Re^3: Non-greedy substitution
by Paladin (Vicar) on Nov 15, 2024 at 19:58 UTC
    The regex engine prioritizes "leftmost". So it will always find the left most place the entire regex will match.

      That's wrong.

      That's the same mindset as saying .*? prioritizes shortest. But we all know that mindset is flawed, since that's the issue at hand.

      For example, your explanation doesn't work for \G(?s:.)*?\K,(.+?)$, which is the OP's pattern with the implicit bits made explicit.