Eyzuky
Eyzuky

Reputation: 1935

Extracting parameters from a method call using REGEX in JAVA

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?

Upvotes: 0

Views: 2164

Answers (4)

Kevin Anderson
Kevin Anderson

Reputation: 4592

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.

Upvotes: 0

dryairship
dryairship

Reputation: 6077

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.

Upvotes: 3

rock321987
rock321987

Reputation: 11032

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*\)

Upvotes: 0

xenoid
xenoid

Reputation: 8904

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...

Upvotes: 0

Related Questions