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?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Eyzuky
  • 1,843
  • 2
  • 22
  • 45

4 Answers4

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.
dryairship
  • 6,022
  • 4
  • 28
  • 54
  • 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. – Dev Gosain Sep 12 '19 at 17:22
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*\)
rock321987
  • 10,942
  • 1
  • 30
  • 43
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...

Community
  • 1
  • 1
xenoid
  • 8,396
  • 3
  • 23
  • 49
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.

Kevin Anderson
  • 4,568
  • 3
  • 13
  • 21