0

I have a pattern like -

public static void myMethod(int Val , String Val){}

so public\static\void\myMethod(int\sVal\s,\sString\sVal)

but if my method have more space than one like public static it fails. So how to make a concrete pattern .

Moreover the part inside the bracket is not working, suggest me the way to resolve.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Abhishek Choudhary
  • 8,255
  • 19
  • 69
  • 128

4 Answers4

4

Use \s+ to match one or more occurrence, and \s* to match zero or more occurrences. Escape the parentheses so that they are not interpreted as grouping operators.

public\s+static\s+void\s+myMethod\s*\(\s*int\s+Val\s*,\s*String\s+Val\s*\)

That said, it looks like you are trying to parse Java code with a regular expression. This is not possible since Java (like the infamous [X]HTML) is not a regular language.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    I would just change the + with a * before the comma – jclozano Jun 22 '11 at 05:03
  • Actually I just want to read the methods from java , I got JavaParser for the same , so now I am doing the assembling job of all parameters , return type and method name to make a complete method and then to revalidate. – Abhishek Choudhary Jun 22 '11 at 05:11
  • Thanks , that worked :) , can you suggest me the importance of a* as when I changed it I found it was accepting only 1 space between the word blocks. – Abhishek Choudhary Jun 22 '11 at 05:15
1

use \s+ instead

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1

A few things, but you are on the right track. Replace your \s with \s+ to indicate 1 or more whitespace characters.

Also, your parens are not working because they are reserved regex characters. You must escape them to have them be literally interpreted

/public\s+static\s+void\s+myMethod\s*\(\s*int\s+Val\s*,\s*String\s+Val\s*\)/
Ben Roux
  • 7,308
  • 1
  • 18
  • 21
1

Try using the "one or more" modifier (+) to match multiple occurrences:

public\s+static\s+void\s+myMethod...
maerics
  • 151,642
  • 46
  • 269
  • 291