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