in reply to Non-greedy substitution

The frugal quantifier means shortest, but still leftmost. You can use a greedy .* at the beginning to consume as much as it can, keep it, and then replace the comma with "and".
#!/usr/bin/perl use warnings; use strict; use experimental qw( signatures ); sub non_oxford_list($s) { $s =~ s/^.*\K,/ and/r } use Test::More tests => 3; is non_oxford_list('A'), 'A'; is non_oxford_list('A, B'), 'A and B'; is non_oxford_list('A, B, C'), 'A, B and C';
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Non-greedy substitution
by Bod (Parson) on Nov 15, 2024 at 21:35 UTC
    The frugal quantifier means shortest, but still leftmost

    Thank you...it was the "leftmost" that was missing from my thought process!

    I think I've understood now 👍

      > I think I've understood now 👍

      maybe this helps, you can reproduce it in the debugger started with perl -de0

      DB<22> p $_ = join "," , A..E A,B,C,D,E DB<23> p m/(,.*,)/ # longest possibility from first comma to last c +omma ,B,C,D, DB<24> p m/(,.*?,)/ # shortest possibility from first comma to next +comma ,B, DB<25> p m/(,.*$)/ # longest possibility from first comma to end of + line ,B,C,D,E DB<26> p m/(,.*?$)/ # shortest possibility from first comma to end o +f line ,B,C,D,E DB<27>

      the regex-engine tries to find a solution step by step:

      • first it tries the first pattern, here "," = comma
      • then it matches "." = all as many times like quantified ( "*" or "*?" )
      • till it matches the next pattern ( "," or "$" = EOL )
      • IFF not all criteria can be met, it'll try to start anew from the next comma, and so on
      The problem with your regex was, that it was already matching from the leftmost comma.

      But all solutions provided by other monks made sure that only the rightmost comma allowed to be a match.

      For instance

      DB<27> p m/(,[^,]*)$/ # comma followed by non-commas till EOL ,E

      The engine will actually try to first match all other commas to the left but always fail because it encounters other commas before reaching the EOL.

      we can actually make the regex display it's intermediate attempts to match while "backtracking"

      DB<32> ; m/(,[^,]*) (?{say $1}) $/x #show all intermediate attempts +to match $1 until it doesn't fail ,B , ,C , ,D , ,E DB<33>

      The difference with non-greedy quantifier *? matching is that the engine goes from shortest to longest attempts while backtracking

      DB<34> ; m/(,[^,]*?) (?{say $1}) $/x #show all intermediate attempts + to match $1 , ,B , ,C , ,D , ,E DB<35>

      Is this clearer now? :)

      HTH!

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        Is this clearer now? :)

        Totally clear now - thanks Rolf for your explanation :)

Re^2: Non-greedy substitution
by Bod (Parson) on Nov 15, 2024 at 21:46 UTC

    Am I right the \K is Keeping everything upto that point and not substituting that part?
    It is everything to the left and not just the 'thing' immediately before /K?

      If you're interested in seeing how the Regex engine steps through a string, you can load Regexp::Debugger. Alternatively, the first edition of Mastering Regular Expressions explains in-depth how Perls RE engine matches and compares that to other engines.