Find commas in the parameters of a function using regex

0

I need to find the occurrence of a string function call, but I need to include the case where there is more than one passed parameter, such as:

Tower.getType(i,j).initialPrice(f,g);

So far I have only been able to formulate the regex when there is only one parameter:

[\w]+([\.]+[\w]+[(]+[\w]*+[)]){2,}+[;]

The code snippet:

public static void verificaMessageChain (String s) {        
    if (s!=null && s.matches("[\w]+([\.]+[\w]+[(]+[\w]*+[)]){2,}+[;]")) {
        System.out.println("\nÉ Message Chain para "+s+"\n");
        splitMessageChain(s); // {0,} equivale a *
    } else if (s!=null && s.matches("[\w] + ([\.] + [\w] + [(] + [\w]* + ([\,] + [\w])* + [)]) {2,} + [;]")) {
        System.out.println("\nÉ Message Chain para "+s+"\n");
        splitMessageChain(s);
    } else {
        System.out.println("\nNão é Message Chain para "+s+"\n");   
    }
}
    
asked by anonymous 25.08.2016 / 22:26

1 answer

0

For this case you can use the following REGEX:

[a-z]\((([a-z]+,?)*)\)

See on REGEX101

Explanation

The logic used was that a method would have the LIT ( PARAMETERS ) pattern, understands that letters can be a letter ( a(params) ), and even more of a letter ( minhaFuncao(params) ), note that it follows the pattern of a letter too ( o(params) ). With that in mind:

  • [a-z]\( - defines that it must have a letter followed by ( .
  • (([a-z]+,?)*) - group 1 that fetches the parameters.
  • ([a-z]+,?)* - the parameters would be letters / words, separated by commas, being able to have infinites or no * .
  • \) - should terminate with ) .

Note

This REGEX is for your specific case, if the parameters are variable, if they are statements like #, you will not be able to use it, even if the parameter is. a string "texto" .

Addendum

If you want to get both [A-Za-z] put the i flag or change the [a-z] sentences to it.

    
26.08.2016 / 14:36