I am working on segment of code which needs to be able to recognize no-args constructor declarations in Java source files. While these declarations need to be syntactically correct, they may still be spread across several lines, for example:
public MyCons()
or
public
MyCons()
or even
public
MyCons
(
)
I am very new to regexp in Java, so I am not sure how to get this working. What I have tried so far is this:
public[\\s|\\n]*MyCons[\\s|\\n]*\\(\\S+\\)
It does not seem to detect cases involving multiple alternating whitespaces and newlines though, as in the last example above. Also, it would be great if I could replace MyCons with a regular expression accepting just a word (although this is not strictly necessary).
How could I solve this?
You need to use backreference since class name and constructor name is always same..
Group 2captures the required constructorYou need to use
DOTALLregex option while matching