Regexp
Since Checkstyle 4.0
Description
This check combines all the functionality provided by RegexpHeader except supplying the regular expression from a file.
It differs from them in that it works in multiline mode. Its regular expression can span multiple lines and it checks this against the whole file at once. The others work in single-line mode. Their single or multiple regular expressions can only span one line. They check each of these against each line in the file in turn.
Note: Because of the different mode of operation there may be some changes in the regular expressions used to achieve a particular end.
In multiline mode...
-
^
means the beginning of a line, as opposed to beginning of the input. - For beginning of the input use
\A
. -
$
means the end of a line, as opposed to the end of the input. - For end of input use
\Z
. - Each line in the file is terminated with a line feed character.
Note: Not all regular expression engines are created equal. Some provide extra functions that others do not and some elements of the syntax may vary. This check makes use of the java.util.regex package; please check its documentation for details of how to construct a regular expression to achieve a particular goal.
Note: When entering a regular expression as a parameter in the XML config file you must also take into account the XML rules. e.g. if you want to match a < symbol you need to enter <. The regular expression should be entered on one line.
Note: To search for parentheses () in a regular expression you must escape them like \(\). This is required by the regexp engine, otherwise it will think they are special instruction characters.
Note: To search for things that mean something in XML, like < you need to escape them like <. This is required so the XML parser does not act on them, but instead passes the correct character to the regexp engine.
Properties
name | description | type | default value | since |
---|---|---|---|---|
duplicateLimit | Control whether to check for duplicates of a required pattern, any negative value means no checking for duplicates, any positive value is used as the maximum number of allowed duplicates, if the limit is exceeded violations will be logged. | int | 0 |
4.0 |
errorLimit | Specify the maximum number of violations before the check will abort. | int | 100 |
4.0 |
format | Specify the pattern to match against. | Pattern | "^$" |
4.0 |
ignoreComments | Control whether to ignore matches found within comments. | boolean | false |
4.0 |
illegalPattern | Control whether the pattern is required or illegal. | boolean | false |
4.0 |
message | Specify message which is used to notify about violations, if empty then the default (hard-coded) message is used. | String | null |
4.0 |
Examples
Default configuration does nothing:
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp"/>
</module>
</module>
An example of how to configure the check to make sure a copyright statement is included in the file:
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format" value="// This code is copyrighted\."/>
</module>
</module>
</module>
Example1:
// This code is copyrighted.
public class Example1 {}
Example2 with violation:
/* violation on first line 'Required pattern missing in a file.' */
/*
* Some Copyright
*/
public class Example2 {}
Your statement may be multiline.
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format"
value="// This code is copyrighted\n// \(c\) MyCompany"/>
</module>
</module>
</module>
Example3:
// This code is copyrighted
// (c) MyCompany
public class Example3 {}
Configure Check to make sure that it appear only once:
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format"
value="// This code is copyrighted\n// \(c\) MyCompany"/>
<property name="duplicateLimit" value="0"/>
</module>
</module>
</module>
Example4:
// This code is copyrighted
// (c) MyCompany
public class Example4 {}
// violation below 'Found duplicate pattern'
// This code is copyrighted
// (c) MyCompany
class Example41 {}
Instead of printing whole regexp that might be unnecessary for user, you can substitute it to some static text:
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format"
value="// This code is copyrighted\n// \(c\) MyCompany"/>
<property name="message" value="Copyright"/>
</module>
</module>
</module>
Example5 with violation:
/*
* violation on first line 'Required pattern 'Copyright' missing in file.'
*/
public class Example5 {}
Configure the check to make sure there are no calls to System.out.println
:
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format" value="System\.out\.println"/>
<property name="illegalPattern" value="true"/>
</module>
</module>
</module>
Example6:
public class Example6 {
private void foo() {
System.out.println(""); // violation, 'Line matches the illegal pattern'
System.out.
println("");
// System.out.println("debug");
// violation above, 'Line matches the illegal pattern'
}
}
Configure to ignore comments:
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format" value="System\.out\.println"/>
<property name="illegalPattern" value="true"/>
<property name="ignoreComments" value="true"/>
</module>
</module>
</module>
Example7:
public class Example7 {
private void foo() {
System.out.println(""); // violation, 'Line matches the illegal pattern'
System.out.
println("");
// System.out.println("debug");
}
}
Configure the check to find trailing whitespace at the end of a line:
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format" value="[ \t]+$"/>
<property name="illegalPattern" value="true"/>
<property name="message" value="Trailing whitespace"/>
</module>
</module>
</module>
Example8:
public class Example8 {
private void foo() {
// violation above 'Trailing whitespace'
}
}
Configure the check to find case-insensitive occurrences of "debug":
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format" value="(?i)fix me\."/>
<property name="illegalPattern" value="true"/>
</module>
</module>
</module>
Note: The (?i) at the beginning of the regular expression tells the regexp engine to ignore the case.
Example9:
public class Example9 {
private void foo() {
// fix me.
// violation above, 'Line matches the illegal pattern.'
}
}
There is also a feature to limit the number of violations reported. When the limit is reached the check aborts with a message reporting that the limit has been reached.
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property name="format" value="(?i)fix me\."/>
<property name="illegalPattern" value="true"/>
<property name="errorLimit" value="1"/>
</module>
</module>
</module>
Example10:
public class Example10 {
private void foo() {
// fix me.
// violation above, 'Line matches the illegal pattern'
}
private void foo1() {
// fix me.
}
}
To configure the check to verify that each file has the multiline header where year could be any digits.
<module name="Checker">
<module name="TreeWalker">
<module name="Regexp">
<property
name="format"
value="// Copyright \(C\) \d\d\d\d MyCompany\n// All rights reserved"/>
</module>
</module>
</module>
Example:
// Copyright (C) 2004 MyCompany
// All rights reserved
public class Example11 {}
To configure the check to verify that each file start with the multiline header, you should append '\A' to 'regexp' at left.
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.regexp