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 | |
by Bod (Parson) on Nov 15, 2024 at 21:38 UTC | |
by ikegami (Patriarch) on Nov 18, 2024 at 01:34 UTC |