Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Extracting parameters from a string with function call

Last post 08-27-2008, 7:34 PM by Aussie Susan. 3 replies.
Sort Posts: Previous Next
  •  08-27-2008, 1:03 PM 45712

    Extracting parameters from a string with function call

    Hello guys!

    I have a string with a function call, and I want to extract the parameters from this.Lets imagine the following example:

    var functionCall = "DOMUtils.setBackgroundColor(this,'#c00');";

    var regExtractRegEx = /([\w\.]+)\(([A-z0-9]*)[,]{0,}/g;

    var matches = new Array();
    var curMatch = null;
                            
    while( (curMatch = regExtractRegEx .exec(functionCall )) != null){
        matches.push(curMatch);
    }

    console.dir(matches);

    the above code returns me the following array:

    [ "DOMUtils.setRowBackgroundColor(this,"#c00"",

       "DOMUtils.setRowBackgroundColor",

       ""#c00""

    ]

     

    but I was expecting something like:

     

    [ "DOMUtils.setRowBackgroundColor(this,"#c00"",

       "DOMUtils.setRowBackgroundColor",

       "this",

       ""#c00""

    ]

     

    Can you please help me?

     Tnks!

     Diogo Jacobs

  •  08-27-2008, 2:19 PM 45713 in reply to 45712

    Re: Extracting parameters from a string with function call

    Isn't it easier to split on one of the following characters: '(', ')' and ','?

     

    But it's an entirely different thing if you want to scan through an entire file an extract this information. Take the following file for example:

    class AClass {

      String s = "methodA(1,2,3);";

      // void mehodB("A","B") {}

      /*
      void  methodC();
      */

      void methodD(char c, int i) {
      }
    }

     

    The only "real" metod is methodD: the rest is just inside comments or inside string literals. If you want this functionality (extarcting from source files), then regex is definatelly NOT the right tool for the job. Really.

  •  08-27-2008, 3:04 PM 45715 in reply to 45712

    Re: Extracting parameters from a string with function call

    There have been long threads detailing the parsing of function calls recently:

    http://regexadvice.com/forums/thread/44620.aspx

    http://regexadvice.com/forums/thread/45445.aspx


  •  08-27-2008, 7:34 PM 45720 in reply to 45712

    Re: Extracting parameters from a string with function call

    Is this a 'cut and paste' copy of your code because I don't see how it can produce the results you say. Your second 'argument' contains 2 single quotes and a hash character which are not included in the '[A-z0-9]' character set definition (both characters occur before the '0' in the ASCII sequence). Also the part that matches each parameter isn't in a repeating group.

    Even so, and assuming that these things have been corrected, the reason you are only seeing the 2nd parameter is because it is the LAST parameter matched. Match groups can only remember the last characters that match (unless you use the .NET 'captures'). Therefore, as your pattern matches each parameter, it will save the matching characters over the top of the previous ones and so leave you with only the last one at the end of the whole matching process.

    A way around this is to use 2 passes: first to find all of the parameters as a single string, and then to match a single parameter multiple times.

    Susan

View as RSS news feed in XML