If you just gotta do this with a regex, try something like (needs Perl version 5.10+):

c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $s = 'ab + (fun1( fun2( 3 +fun3 ( 4+(5+6) ))) ) + x + f ( 1 + 2 )' +; print qq{'$s'}; ;; $s =~ s{ ( \( (?: [^()]++ | (?1) )* \) ) } { (my $ns = $1) =~ tr/ \t\n\f//d; $ns; }xmsge; print qq{'$s'}; " 'ab + (fun1( fun2( 3 +fun3 ( 4+(5+6) ))) ) + x + f ( 1 + 2 )' 'ab + (fun1(fun2(3+fun3(4+(5+6))))) + x + f (1+2)'
This uses Perl version 5.10 Extended Patterns  (?1) (essential; see (?PARNO)) and the  ++ "possessive quantifier" (inessential; could use a  (?>pattern) "atomic" group). If you have Perl version 5.14+, the  tr/// expression in the replacement block can simply be
    { $1 =~ tr/ \t\n\f//dr; }
(note the  /r switch) with no need for substitute-on-copy.

I doubt this regex is the most optimal. I'd like to come up with a regex that just matches  \s+ within a nested paren group and replaces with the empty string, but haven't figured this out yet.

Update: This is a little neater, more elegant, but still not quite what I was hoping for:

c:\@Work\Perl\monks\iaw4>perl -wMstrict -le "use 5.010; ;; my $s = 'ab + (fun1( fun2( 3 +fun3 ( 4+(5+6) ))) ) + x + f ( 1 + 2 )' +; print qq{'$s'}; ;; $s =~ s{ [(] (?: [^()]++ | (?0) )* [)] } { (my $ns = ${^MATCH}) =~ s/\s+//g; $ns; }xmsgpe; print qq{'$s'}; " 'ab + (fun1( fun2( 3 +fun3 ( 4+(5+6) ))) ) + x + f ( 1 + 2 )' 'ab + (fun1(fun2(3+fun3(4+(5+6))))) + x + f (1+2)'
Note that the  /p modifier is needed here because  ${^MATCH} is used. And again, the replacement expression becomes a bit simpler with the use of the  /r modifier of Perl 5.14+ on the  s/// operation.


Give a man a fish:  <%-{-{-{-<


In reply to Re: paren formatter for function calls? (updated) by AnomalousMonk
in thread paren formatter for function calls? by iaw4

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.