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!


In reply to Re: Need help with substitue /g by AnomalousMonk
in thread Need help with substitue /g by stephanm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.