0

I want to match some strings that doesnot contain any semicolon at the end of a line or middle of the matched Strings.

If a line :Someword; public void methodName() or only public void methodName()

I want to match:public void methodName()

but if the line contain semicolon in the last or middle of my matched String like :

Someword; public void; methodName(); or only public void methodName(); it does not matched any string. As far I tried in java:

String pattern = "(public)\\s+[a-zA-Z]*\\s+(\\bmethodName\\b)\\s*\\(\\)[^;]*$";
      Matcher methodMatcher = Pattern.compile(pattern).matcher(classContent); //classContent is a java file
        while (methodMatcher.find()) {
         System.out.println("methodname="+methodMatcher.group());
        }
Sanzida
  • 59
  • 6
  • 1
    What about strings like `methodName();//comment`? It doesn't end with `;` so should it be matched or not? Anyway this looks like [XY problem](https://meta.stackexchange.com/q/66377). – Pshemo Sep 05 '21 at 14:41
  • 1
    Are you trying to parse Java with regex? – Andy Turner Sep 05 '21 at 14:45
  • @Pshemo no it should not match. I want to capture that has a methodbody.Not abstract method – Sanzida Sep 05 '21 at 14:46
  • @AndyTurner I want to capture valid method that has a body from a java file – Sanzida Sep 05 '21 at 14:47
  • @AndyTurner I want to match these strings **public void methodName()** if the line is: **Someword; public void methodName()** – Sanzida Sep 05 '21 at 14:50
  • 4
    Regex is not good tool for parsing (there are too many valid cases which you may miss with regex, or invalid which you may accept). Parsers are best tools for parsing. Possibly related: [Java : parse java source code, extract methods](https://stackoverflow.com/q/2206065) – Pshemo Sep 05 '21 at 14:51
  • 1
    it's valid Java syntax to put the ending semicolon in an new line – P.J.Meisch Sep 05 '21 at 14:52
  • @Pshemo I am going through a project in my university where I could not use any extra jar file. – Sanzida Sep 05 '21 at 15:02

1 Answers1

0

Use

^(?:\w+;)?\s*public\s+\w+\s+methodName\s*\(\)\s*$

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    ;                        ';'
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  public                   'public'
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  methodName               'methodName'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  \)                       ')'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

Sample code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Example {
    public static void main(String[] args) {
        final String regex = "^(?:\\w+;)?\\s*public\\s+\\w+\\s+methodName\\s*\\(\\)\\s*$";
        final String string = "Someword; public void methodName()\n"
     + "public void methodName()\n\n"
     + "Someword; public void; methodName();\n"
     + "public void methodName();";
        
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
        
        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            
            for (int i = 1; i <= matcher.groupCount(); i++) {
                System.out.println("Group " + i + ": " + matcher.group(i));
            }
        }
    }
}
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37