Find comma in the parameters of a function using regex

Asked

Viewed 157 times

0

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

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

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

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

The excerpt from the code:

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");   
    }
}
  • 2

    Note the expression metodo(a.b(c, (d + e.f(g, h(i) + j, k) * m), n.o(p, q, r.s().t(u.v())), w), x, y + z) - The conclusion is that to handle it properly you would need a context-free language, not regular.

  • But what for a simple one? As a method(a, b, c), I do not need to be able to analyze all the cases, but some simple at least.

1 answer

0


For this your 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 pattern LETTERS(PARAMETERS), understands that letters can be a letter (a(params)), and even if it’s more than one 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 comma, may have infinite or no *.
  • \) - must terminal with ).

Note

This REGEX serves for your specific case, if the parameters are variables, if they are sentences like the @Victor said in the commentary, you won’t be able to use it, even if the parameter is a string "texto".

Addendum

If you want me to take so much [A-Za-z] flag i or change the sentences [a-z] for the same.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.