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

Regex to parse parameters from a String function call

Last post 08-20-2008, 4:53 PM by ddrudik. 20 replies.
Page 1 of 2 (21 items)   1 2 Next >
Sort Posts: Previous Next
  •  08-19-2008, 3:22 PM 45445

    Regex to parse parameters from a String function call

    Hello,

     This is my first post to this forum

    I need to find the right Regex to use for the Java split method, in order to split a list of parameters located within a String function call. The function call is a JavaScript function call which means that its parameters can be literals or expressions or other function calls. What makes this particularly difficult is that in JavaScript there is an Array literal and an object literal, which both contain commas (commas of course also separate parameters from each other).

     Assuming I wanted to end up with an Object[] or String[]  of parameters from the following string function call, what regex would I pass to .split() in Java? Any help is appreciated! Also, I can only assume that someone has already written this somewhere no?! Anyone know where I might find this regex canned?

     String function call:

     function('mYstring\'\'\'s', 1e12 , ['A,,,'],application.beep(1,2,3,'f,oo',[1,2,'strin,,,g'], {name: 'foo', test: 1e12}),'blah \n aaa', {myObject: [1,2,3]});

     

    Java to split String function call into Object[]:

      Object[] params = functionCall.substring((functionCall.indexOf("(") + 1), functionCall.lastIndexOf(")")).split("SUPER-SMART-REGEX-GUY-INSERT-REGEX-HERE :-)");

     

  •  08-19-2008, 3:49 PM 45448 in reply to 45445

    Re: Regex to parse parameters from a String function call

    It would be for you to show us what matches you want from the source string specifically and then we can try to determine what pattern is required given those matches.
  •  08-19-2008, 4:37 PM 45449 in reply to 45448

    Re: Regex to parse parameters from a String function call

    OK...

     

    From the String I would want the following matches:

     1). 'mYstring\'\'\'s'

    2). 1e12

    3). ['A,,,']

    4). application.beep(1,2,3,'f,oo',[1,2,'strin,,,g'], {name: 'foo', test: 1e12})

    5). 'blah \n aaa'

    6). {myObject: [1,2,3]}

     

    Thanks for the reply! 

     Basically commas enclosed within [],(),'' or {} should not be split on. Others should be.

  •  08-19-2008, 5:42 PM 45451 in reply to 45449

    Re: Regex to parse parameters from a String function call

    Note I tested this in PHP, but once I matched the function with:

       Pattern re = Pattern.compile("\\w+(\\((?:(?>[^()]+)|(?1))*\\));");

    And using group 1 with the leading ( and trailing ) removed with a substring function, I received the desired matches with:

      Pattern re = Pattern.compile("[^\\s,]*(?<!\\\\)\\(.*?(?<!\\\\)\\)|(?<!\\\\)\\[.*?(?<!\\\\)\\]|(?<!\\\\){.*?(?<!\\\\)\\}|(?<!\\\\)'.*?(?<!\\\\)'|(?:(?!,)\\S)+");
     

    Based on the limited sample.  Although quite possibly there's a better way to do this.


  •  08-19-2008, 7:50 PM 45455 in reply to 45451

    Re: Regex to parse parameters from a String function call

    First off thank you for your reply and for you time on this. I really appreciate it! 

     

    --> Regarding matching the function and substringing the params sans the parentheses before applying the regex...understood.

    --> Regarding the pattern, I cannot compile it in Java 1.5. It thows "Illegal repetition near index 72 [^\\s,]*(?<!\\\\)\\(.*?(?<!\\\\)\\)|(?<!\\\\)\\[.*?(?<!\\\\)\\]|(?<!\\\\){.*?(?<!\\\\)\\}|(?<!\\\\)'.*?(?<!\\\\)'|(?:(?!,)\\S)+ ^"

     

    Any ideas?

  •  08-19-2008, 9:17 PM 45457 in reply to 45455

    Re: Regex to parse parameters from a String function call

    Raw Match Pattern:
    [^\s,]*(?<!\\)\(.*?(?<!\\)\)|(?<!\\)\[.*?(?<!\\)\]|(?<!\\){.*?(?<!\\)}|(?<!\\)'.*?(?<!\\)'|(?:(?!,)\S)+

    Java Code Example:
      Pattern re = Pattern.compile("[^\\s,]*(?<!\\\\)\\(.*?(?<!\\\\)\\)|(?<!\\\\)\\[.*?(?<!\\\\)\\]|(?<!\\\\){.*?(?<!\\\\)}|(?<!\\\\)'.*?(?<!\\\\)'|(?:(?!,)\\S)+");

    Any better?  It's likely how Java treats the (?<!\\) entries or other escaping.


  •  08-19-2008, 11:00 PM 45461 in reply to 45457

    Re: Regex to parse parameters from a String function call

    Thanks again, but this still throws an error and unfortunately I don't have the regex expertise to diagnose where exactly the error is.

     "Illegal Repetition" 

  •  08-20-2008, 8:33 AM 45479 in reply to 45461

    Re: Regex to parse parameters from a String function call

    I needed to escape the {'s. After I did that, and after I used the Java version of the string the pattern compiled. Although I do not get the appropriate matches...close but no cigar.a

     

    I am using: http://www.fileformat.info/tool/regex.htm to test the java version of the pattern: [^\\s,]*(?<!\\\\)\\(.*?(?<!\\\\)\\)|(?<!\\\\)\\[.*?(?<!\\\\)\\]|(?<!\\\\)\{.*?(?<!\\\\)\}|(?<!\\\\)'.*?(?<!\\\\)'|(?:(?!,)\\S)+

    against the original test string: 'mYstring\'\'\'s', 1e12 , ['A,,,'],application.beep(1,2,3,'f,oo',[1,2,'strin,,,g'], {name: 'foo', test: 1e12}),'blah \n aaa', {myObject: [1,2,3]

     

    Any ideas? 

  •  08-20-2008, 9:51 AM 45485 in reply to 45479

    Re: Regex to parse parameters from a String function call

    The pattern in Java code that I supplied should work fine, the regex tester you used escapes all \ with another \ which is not necessary with my pattern since I already escaped \'s for Java.

    I entered this in that tester (note the \'s are not double-escaped) and it tests fine based on your sample:

    [^\s,]*(?<!\\)\(.*?(?<!\\)\)|(?<!\\)\[.*?(?<!\\)\]|(?<!\\)\{.*?(?<!\\)\}|(?<!\\)'.*?(?<!\\)'|(?:(?!,)\S)+


  •  08-20-2008, 11:08 AM 45489 in reply to 45485

    Re: Regex to parse parameters from a String function call

    I see that this sequence does indeed match at the site I sent you (cool!!), but how can it be represented as a string. You mentioned that you already escaped all the appropriate characters, but the pattern is not a valid Java String (or char sequence for that matter). On the site I sent you they spit back a string, but as you pointed out it quotes overzealously and when I try to compile that String it fails to match.

     

    So your raw pattern matches beautifully, but is not a valid string or char sequence in Java, so I can't split on it or compile it. When I try to quote it manually I break it. :-(

    [^\s,]*(?<!\\)\(.*?(?<!\\)\)|(?<!\\)\[.*?(?<!\\)\]|(?<!\\)\{.*?(?<!\\)\}|(?<!\\)'.*?(?<!\\)'|(?:(?!,)\S)+

  •  08-20-2008, 11:10 AM 45490 in reply to 45489

    Re: Regex to parse parameters from a String function call

    BTW...pretty impressive bit of REGEX!
  •  08-20-2008, 11:13 AM 45491 in reply to 45489

    Re: Regex to parse parameters from a String function call

    europe72:

    So your raw pattern matches beautifully, but is not a valid string or char sequence in Java, so I can't split on it or compile it. When I try to quote it manually I break it. :-(

    You can either use the Java code I provided (scroll up to see the Java code sample) or use the escaped version the Java regex tester supplied when you tested my raw pattern.


  •  08-20-2008, 11:27 AM 45492 in reply to 45490

    Re: Regex to parse parameters from a String function call

    europe72:
    BTW...pretty impressive bit of REGEX!

    It might only work for your limited sample, note that given the order of the alternations it's important that your functions do not have ( ) [ ] { } ' ' groups nested in an order other than specified.

    For example, the pattern attempts to match in this order:

    1. any non-whitespace non-comma character followed by an unescaped ( until the next unescaped )
    2. an unescaped [ until the next unescaped ]
    3. an unescaped { until the next unescaped }
    4. an unescaped ' until the next unescaped '
    5. any non-whitespace non-comma characters

    Let's say by accident you have something like:

    {test 123,[test:1],test}, 'test {test}', [test (test) test]

    It will parse incorrectly since it will match the ( ) [ ] { } ' ' groups in the wrong order, finding submatches of the intended groups.


  •  08-20-2008, 11:34 AM 45493 in reply to 45492

    Re: Regex to parse parameters from a String function call

    Regarding trying to use the Java string you provided, I can't b/c it does not compile. It is a valid String, but does not compile/match anything. The raw match matches everything, but is not a valid String.

     

    Regarding the order of things..in JavaScript, which is what these function call string are, an array [] can contain an object {} and an object {} can contain an array []. Either one can contain a function call (). e.g.

    {name: test 123,foo: [1,,,],bar: myFunctioncall([1,2,3], {name: blah, foo: [1,2,3], function: myFunctionCallAgain(etc.....)})} 

     pattern must be order agnostic

    Is it safe to assume the regex you have provided can't handle these cases or do I misunderstand what you are saying.

     

    thanks! 

  •  08-20-2008, 11:42 AM 45494 in reply to 45493

    Re: Regex to parse parameters from a String function call

    My regex cannot handle the varied conditions then, I'm not sure how a pattern would be able to do that effectively.


Page 1 of 2 (21 items)   1 2 Next >
View as RSS news feed in XML