Friday, 15 February 2013

Regex: Pattern to find Java class in .java file -



Regex: Pattern to find Java class in .java file -

i want find class' name when reading through .java file. i'm returning no match right using regular expression:

\\s*[public][private]\\s*class\\s*(\\w*)\\s*\\{

here code far:

import java.io.*; import java.util.*; import java.util.regex.*; public class hw4solution { public static void main(string [] args){ //prompt user path file. file file = null; scanner pathscan = new scanner(system.in); while (file == null || !file.exists()) { system.out.print("enter valid file path: "); file = new file(pathscan.next()); } pathscan.close(); system.out.println("file: " + file.getpath() + " found."); //read file line line buffered reader stringbuffer componentstring = new stringbuffer(100); string currentline; bufferedreader bufferedreader = null; seek { bufferedreader = new bufferedreader(new filereader(file.getpath())); } grab (filenotfoundexception e) { e.printstacktrace(); } //todo: find class declarations //todo: find superclasses //todo: find instance variable declarations //todo: find method signatures //todo: find access modifier //todo: find homecoming type //todo: find method name //creating patterns for! //class declarations pattern classdeclarationpattern = pattern.compile("\\s*[public][private]\\s*class\\s*(\\w*)\\s*\\{"); seek { while((currentline = bufferedreader.readline()) != null){ matcher classdeclarationmatcher = classdeclarationpattern.matcher(currentline); if(classdeclarationmatcher.group(1) != null){ componentstring.append("found class declaration: " + classdeclarationmatcher.group(3) + "\n"); /*if(classdeclarationmatcher.group(5) != null){ componentstring.append("\tsuperclass: " + classdeclarationmatcher.group(5) + "\n"); }*/ system.out.println(classdeclarationmatcher.group()); } } } grab (ioexception e) { e.printstacktrace(); } finally{ try{ if (bufferedreader !=null) { bufferedreader.close(); } } catch(ioexception e){ e.printstacktrace(); } } system.out.println(componentstring.tostring()); } }

i want able determine if class declaration has superclass , i'm having plenty problem (which shouldn't) getting name of class.

[public] not matching think should. character class, matches either of characters in public. should utilize pipe match alternate words.

you can utilize next regex, extended consider super classes or interface: -

pattern.compile("\\s*(public|private)\\s+class\\s+(\\w+)\\s+((extends\\s+\\w+)|(implements\\s+\\w+( ,\\w+)*))?\\s*\\{");

note that, should utilize \\s+ + quantifier when matching space between public|private , class keyword. because expect @ to the lowest degree 1 space there. \\s* match 0 space, not correct, since publicclass not valid.

apart that, there can more options consider here. static inner classes, or generic classes, require minor modification in there, can on own. i've given little insight on how approach.

also, 1 more of import thing. before can result group, should phone call matcher#find() method actual matching.

java regex

No comments:

Post a Comment