Reputation: 3271
Is it possible to write a regex, which extractst the package path in a .java file?
Something like this:
package java.awt.event;
// java code
to this
java.awt.event
which i than can resolve to the folder location?
java/awt/event/
I don't know which characters a package can have, or if it's possible to add more than one package.
Upvotes: 3
Views: 4237
Reputation: 1087
I know this is delayed but i would like to contribute this one i made which works reasonably well specifically for Android package names convention.
(^[a-z]+[.][a-z]+[.][a-z]+($|[.][a-z]+$))
Upvotes: 0
Reputation: 11
This regexp works perfectly :
package\\s([a-zA-Z]\\w*)(\\.[a-zA-Z]\\w*)*;"\
to match :
Upvotes: 1
Reputation: 32969
"package\\s+([a-zA_Z_][\\.\\w]*);"
There can only be one package at the beginning of a java file. Only word characters and "." are allowed.
"package\\s+" => the work "package" followed by at least one space
"(" => start of capture
"[a-zA-Z_]" => first character of package (doesn't include numeric digits)
"[\\.\\w]*" => any number of word characters (a-zA-Z0-9_) plus the "." character
")" => end of capture
";" => end of line
Really this is not the best since it does not enforce that the first character of each folder not be a digit however it will work without fully enforcing rules. This being the case the following might be easier:
"package\\s+([\\w\\.]+);"
Upvotes: 12
Reputation: 48216
you can have only 1 package declaration and allowed syntax is the same as normal identifiers
"package ([\\w&&\\D]([\\w\\.]*[\\w])?);"
capture group 1 will have the package replace the . with file separators and you're there
Upvotes: 2