0

i want to get class name from a string .for example public class hello extends jframe .then i want to get hello as class name .but if String is public class hello and also i want to get hello.

i write a regex and it's work fine . see here online preview .

this is my regex

(public)*\s*class\s+(\S+)\s+

preview

enter image description here

as you can see it works ..it capture class names . (red color -> group 2)

but in java m.matches() returns false because whole string doesn't match but a part .so according to this answer , i used hitEnd() method but it doesn't work either ?? how can i capture class name when even string exactly doesn't match but partially

String cstring = "public class hello extends jframe ";
Pattern classPattern = Pattern.compile("(public)*\\s*class\\s+(\\S+)\\s*");
Matcher m = classPattern.matcher(cstring);
m.matches();
if (m.hitEnd()) {
    System.out.println("found");
    String className = m.group(2);
    System.out.println(className);
}

output is nothing.

Community
  • 1
  • 1
while true
  • 816
  • 1
  • 11
  • 26

3 Answers3

2

See comments in the code and explanation below:

String cstring = "public class hello extends jframe ";
Pattern classPattern = Pattern.compile(".*?class\\s+(\\S+)\\s*"); // regex was changed a bit
Matcher m = classPattern.matcher(cstring);
//m.matches();  // <-- no need for that 
if (m.find()) { // use find() instead of m.hitEnd()
    System.out.println("found");
    String className = m.group(1);
    System.out.println(className);
}

OUTPUT

found
hello

Basically I removed the (public) from the regex and used the "1" matched group (zero is the whole expression in case there is a match). Your regex could be used as well, but then you'll have to use the seconded matched group as (public) will be come the first.

In addition to that I removed m.matches() which is not needed and replaced the m.hitEnd() with m.find() which should be used to iterate the matched results.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

You need to call find() to make the engine find its match before trying to access it.

String s  = "public class hello extends jframe";
Pattern p = Pattern.compile("public\\s*class\\s+(\\S+)");
Matcher m = p.matcher(s);
if (m.find()) {
    System.out.println("found");
    String className = m.group(1);
    System.out.println(className);
}

Ideone Demo

hwnd
  • 69,796
  • 4
  • 95
  • 132
1

Try if (m.find())

public static void main(String[] args) throws Exception {
    String cstring = "public class hello extends jframe ";
    Pattern classPattern = Pattern.compile("(public)*\\s*class\\s+(\\S+)\\s*");
    Matcher m = classPattern.matcher(cstring);

    if (m.find()) {
        System.out.println("found");
        String className = m.group(2);
        System.out.println(className);
    }
}

Results:

found
hello

Also, your capture group (\\S+) will match any non-whitespace character which can result in invalid class names. Consider changing that capture group to ([a-zA-Z_$]\\w*) to capture valid class names.

Shar1er80
  • 9,001
  • 2
  • 20
  • 29