Reputation: 166
I am currently working with checkstyle for the first time and got a little problem. I want that checkstyle validates if there is exact one blank line between package declaration and following content. I couldnt find a module which is offering this kind of validation, or at least none which doesnt also check if there is a blank line between the package declaration and the content above.
So I decided to use the regexp-module and write a regular expression to achieve this kind of validation. I tried to cover all possible voilations of my rule and in some online validators where you could try out your regexp it worked with no problem.
Heres the regular Expression:
.+package.*;|package.*;.*\S|package.*;.*[\r\n].*\S|package.*;[\r\n]{3,}
Now when I try to import this in my check configuration I always get the message that my rule is getting violated even though the code is correct.
Here some example code:
package just.some.package;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
...
Normally this code shouldnt trigger any problems but it always does as soon I try it in the checkstyle Plugin within my IDE. The snippet of the regexp which is causing the problem seems to be this one:
package.*;(\r|\n){3,}
It should just check if there are more than two line breaks after package declaration but it does also trigger an error if there are exactly two like in the example.
Can someone explain me what the problem is?
With best regards
Demli
Upvotes: 1
Views: 208
Reputation: 627087
You may use the \R
construct to match line breaks,
package.*;\R{3,}
Both [\r\n]{3,}
and (\r|\n){3,}
match because you have CRLF endings and that means there are 4 CR/LF chars between the two lines separated with an empty line.
Alternatively, you may specify (?:\r\n){3,}
pattern if you work with Windows line ending style.
Upvotes: 1