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

Your mental model of what «.+?» does is severely flawed. For starters, it doesn't permit patterns to have multiple subpatterns that can match substrings of different lengths.

«.+?» does not mean "the shortest possible match within the string that starts with a comma".

«.+» means "one or more non-LF characters, trying in order of decreasing length", and
«.+?» means "one or more non-LF characters, trying in order of increasing length".

Note that lack of mention of comma. «.+?» doesn't do any checks related to commas. The comma is matched independently.

Replies are listed 'Best First'.
Re^4: Non-greedy substitution
by Bod (Parson) on Nov 15, 2024 at 21:38 UTC
    Your mental model of what «.+?» does is severely flawed

    Well, yes...hence the need to ask the question...

    «.+?» doesn't do any checks related to commas

    But I included the comma in my example...perhaps I could have formatted it so it was more prominent.

      I know. But you said you thought «,.+?$» searches for "the shortest possible match within the string that starts with a comma". Since neither «,» nor «$» searches for the shortest possible anything (starting with a comma or otherwise), that means you though «.+?» did that. «.+?» doesn't search for commas, which it would need to do to find "the shortest possible match within the string that starts with a comma".