Having a function call in the argument list is not your only problem. An argument can be any arbitrary expression, and many of them include "own" commas. The simplest is probably just a string; funca("this is just one argument, I think"). But then there are many other common argument expressions, like anonymous arrays or hashes, method calls, map/grep that doesn't use a "regular" function syntax and has a block that may and often does use a comma (in any form or shape), which leads me into mentioning the => operator. Etc etc. It's up to you to decide how detailed you want to be. You'll quite likely end up with a little parser, far beyond simple one-line regexes. For that I recommend using Parse::DecDescent, or at least looking into it to see if it fits you. Parse::RecDescent provides means to extract code blocks, several quote constructs, including qr//, qq!!, etc.

If you decide to stay with regexes or a hand-rolled parser, you should look into recursive regexes, see the perlre manpage.

Another approach would be to first try to find something that looks like a function call, by scanning for function names and then just grabbing everything inside the parenthesis. You'd do this by "balanced matching", i.e. you match all of "(1,(2,3),4)". Again, look in perlre for an example of this. But you need to specifically handle strings, since they might contain any kind of brackets. After than you can try to parse the argument elements. If you fail, handle the specifically. Then you'll at least be aware of that there are "strange" function calls.

This method is far from perfect, but perhaps it's good enough for you.

Hope I've helped,
ihb

In reply to Re: regex assistance for parsing arg list by ihb
in thread regex assistance for parsing arg list by denap

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.