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

Solution:
sub join_list { return "none" if !@_; # ??? my $last = pop; return $last if !@_; return join( ", ", @_ ) . " and " . $last; }

An interesting solution.

However, in my quest to understand what is going on, I tried forcing the match to be non-comma characters and came up with this which produces the desired behaviour.

perl -e "my $test = join ', ', ('A', 'B', 'C');$test =~ s/,([^,]+?)$/ +and$1/; print $test;"

I still don't understand why the original doesn't work. Surely ,.+?$ is the shortest possible match within the string that starts with a comma and ends at the end of the line...

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

    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.

      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".