0

I have this REGEX:

([A-Za-z]+[\w_]*)\s*\(([[A-Za-z]+[\w_]*\s,?]*)

This REGEX is supposed to find strings like this:

foo(param1,param2,param3......)

Where the first group is the name (it must start with a letter), after that comes the second group which I am not sure about. The problem is that I do not know how many parameters I will receive. The second part is suppose to find a concatenation of zero or more parameters, all in the same format [A-Za-z]+[\w_]. I tried adding a [] around it and a * at the end. How will I be able to match and extract all the parameters into a array list? Is it even a correct REGEX syntax?

2
  • will there be parameters in which function is passed? Commented Jun 5, 2016 at 12:40
  • Why are you using a regex, and not a java parsing library? Commented Jun 5, 2016 at 13:06

4 Answers 4

3

You can use this regex:

([a-zA-Z][a-zA-Z0-9_$]+)\s*\(\s*([a-zA-Z0-9_$]+(?:\s*,\s*[a-zA-Z0-9_$]+)*)\s*\)
  • In the part ([a-zA-Z][a-zA-Z0-9_$]+)
    • [a-zA-Z] is for the first charcater - which has to be a letter.
    • [a-zA-Z0-9_$]* is for the rest of the name - which may include letters, numbers, _ or $.
  • \s* for spaces. (Some people put them.)
  • \( for (.
  • \s* is again for spaces.
  • [a-zA-Z0-9_$]+ is for the first parameter.
  • (?:\s*,\s*[a-zA-Z0-9_$]+)* is for the rest of the parameters.
    • \s* is again for spaces.
    • , is for matching ,
    • \s* is again for spaces.
    • [a-zA-Z0-9_$]+ is for parameter names.
    • The beginning (?: and )* finds as many extra parameters as it can.
  • \s* is again for spaces.
  • \) is for the closing bracket.
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer is perfect. Can you tell what if the arguments are surrounded by single qoute. For Example : method('arg1', 'arg2', 'arg3') Match group should return arg1 arg2 arg3.
0

You can't (see here).

The best you can do is come up with a regexp(*) that will catch all the parameters in one group, and then split that with further code.

(*) something like (unchecked):

([A-Za-z]+[\w_]*(\s*,\s*[A-Za-z]+[\w_]*)*)

Note that if this is supposed to handle real Java code, parameters can be string and number literals, function/method calls or complete expressions, so a single regexp won't cut it anyway...

Comments

0

You can use this regex

([A-Za-z]\w*)\s*\(\s*[A-Za-z]\w*\s+[A-Za-z]\w*\s*(?:,\s*[A-Za-z]\w*\s+[A-Za-z]\w*\s*)*\)
                 <----------------------------2nd part--------------------------------->

Regex Breakdown (for 2nd part)

\( #Match ( literally
  \s* #Match the spaces after (
  [A-Za-z]\w* #Match return type like int, float, String
  \s+ #Match at least one space
  [A-Za-z]\w* #Match variable names
  \s* #Match if there are any spaces
    (?:
      , #Match , literally
      \s* #Match spaces
       [A-Za-z]\w* #Match return type like int, float, String
       \s+ #Match at least one space
       [A-Za-z]\w* #Match variable names
       \s* #Match any number of spaces
    )* #Repeat this 0 or more times
\) #Match ) literally

Regex Demo

Above will require at least one parameter. If you don't want any parameter, you can use

([A-Za-z]\w*)\s*\((?:\s*[A-Za-z]\w*\s+[A-Za-z]\w*\s*(?:,\s*[A-Za-z]\w*\s+[A-Za-z]\w*\s*)*)?\s*\)

Comments

0

First of all, you'd want to write (XXX)* to say that the XXX can repeat zero or more times, NOT `[XXX]*'.

That being said, however, you can't actually write a single regex to do what you have in mind; regexes aren't that powerful. If you are especially keen to work with regexes for some reason, have a look at java.util.regex.Matcher or java.util.Scanner. For a quick-and-dirty solution, just use String.indexOf to locate the delimiters and String.substring to extract the words from between them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.