I am currently developing a corrector for java in my text editor. To do so I think the best way is to use Pattern to look for element of java syntax (import or package declaration, class or method declaration…). I have already written some of these pattern:
private String regimport = "^import(\\s+)(static |)(\\w+\\.)*(\\w+)(\\s*);(\\s*)$",
regpackage="^package(\\s+)[\\w+\\.]*[\\w+](\\s*);(\\s*)$",
regclass="^((public(\\s+)abstract)|(abstract)|(public)|(final)|(public(\\s+)final)|)(\\s+)class(\\s+)(\\w+)(((\\s+)(extends|implements)(\\s+)(\\w+))|)(\\s*)(\\{)?(\\s*)$";
It’s not very difficult for now but I am afraid it will take a long time to achieve it. Does someone know if something similar already exists?
Incorrect. Regular Expression patterns cannot adequately identify Java syntax elements. That is why the much more complex parsers exist. For a simple example, just imagine how you would you avoid the false match for a reserved word inside a comment, such as following
But if you are very keen to use regular expressions, and willing to spend lot of effort, look at
Emacsfont-lock-mode, which uses regular expressions to identify and fontify syntax elements.PS: The “lot of effort” I mention refers to learning how
Emacsworks, readingelispcode and translatingEmacsregexp to Java. if you already know all that then you will need less effort.