in reply to Need help with substitue /g

This may be overkill, but here's an approach to controlling which of several parenthesized digit groups gets its digits quoted (requires  \K from 5.10):

use warnings; use strict; my $paren_grp = qr{ \( \d+ (?: , \d+)* \) }xms; my $non_paren = qr{ [^(] }xms; for my $n (0 .. 3) { my $s = '(1) XXX (123) YY YY (1,22,333) Z Z Z (22,1)'; $s =~ m{ \A $non_paren* (?: $paren_grp $non_paren*){$n} \( }xmsg; $s =~ s{ \G ,? \K (\d+) }{'$1'}xmsg; print qq{skip $n :$s: \n}; }

Output:

skip 0 :('1') XXX (123) YY YY (1,22,333) Z Z Z (22,1): skip 1 :(1) XXX ('123') YY YY (1,22,333) Z Z Z (22,1): skip 2 :(1) XXX (123) YY YY ('1','22','333') Z Z Z (22,1): skip 3 :(1) XXX (123) YY YY (1,22,333) Z Z Z ('22','1'):

Update: I think it should be possible to integrate the two-step  m//g; s///g; approach entirely into a single  s///g; regex (possibly using 5.10's backtracking control verbs), but I can't quite figure out how – and I'm not sure it would be worth the effort except to satisfy my curiosity!