I feel pretty good with my regex foo but am running into a difficult one that I could use some help on.

I have a string that needs to be reformatted and it contains functions that can call functions. e.g.

func2(func1(input1, input2), input3)

But it does not matter which order they are called in. So, it could also be

func1(input1, func2(input2, input3))

So, my dilemma is that if I do a regex to find func1 and replace it then I am left with func2 to deal with. The order matters as to the matching of parens. I have not yet figured out how to get a matching () regex to work properly and have read all of the posts here already.

Here is my code to date:

while (<>) { $line = $_; $line =~ s/func1\((.*?)\)/proc1{$1}/g; $line =~ s/func2\((.*?)\)/FUNC $1 END/g; $line =~ s/{/(/g; $line =~ s/}/)/g; print $line; }

The problem is that I get this when I run it I get this:

proc1(input1, FUNC input2, input3) END FUNC input1, proc1(input2, input3) END
rather than
proc1(input1, FUNC input2, input3 END) FUNC input1, proc1(input2, input3) END

Just in case it is not easy to tell the difference I am getting ") END" rather than "END)".

Any help or pointers on this?


In reply to Matching parens on a regex is beating me by bfdi533

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.