These checks work by applying regular expressions.
This check applies a regular expression to the names of files. Depending on the configuration, a warning is logged if a required match is not found, or if an illegal match is found.
This is useful for situations such as:
By default, this check flags leading and trailing spaces in file names.
The check works like this:
selection
are checked. Leave blank to include all files.regexp
is matched against each selected file name. What part of the file name it is applied to, and how the result is interpreted is governed by the check properties...
elements etc.).
unrestricted^(?:\s+.*|.*?\s+)$
regexp
finds required or illegal matches. required
means that all selected files must match the expression. illegal
means that they must not.
illegal
true
, only the simple name of the file will be checked against the pattern specified by regexp
; if false
, the entire canonical path will be checked.
Note that this option applies only to the pattern specified by regexp
; the selection
property is always treated as if simple=false
.
true
Since this check is a FileSetCheck, it also inherits the fileExtensions
property, which may be configured independently of selection
. In that case, both properties must match (e.g. fileExtensions
and selection
, or either of the two if one is missing).
In addition to the properties, optionally adding a message
element may benefit this check to make the warning easier to understand. The message key depends on the value of the mode
option. If mode=required
, the message key regexp.filepath.required
is used. If mode=illegal
, the message key regexp.filepath.illegal
is used. The message text can make use of placeholders {0}
(the file name as used by the matcher) and {1}
(the regular expression used by the matcher).
By default, the check detects leading and trailing spaces in file names. It is recommended to still add a custom message as shown, but that’s optional.
To configure the check to ensure that Java files reside in Java source folders, not resource folders:
This check is also useful to enforce arbitrary naming conventions. In the following example, we require all HTML files in a folder html/view to start with the prefix view_
:
To configure the check to ban GIF files in favor of PNG:
The (?i)
at the start of the selection
expression turns on case insensitivity, so that .gif
, .GIF
, or even .Gif
are all matched.
Important: This check goes directly under Checker, not under TreeWalker.