Regexp v2.0.0

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.

The Checkstyle team liked this check so much that they added it to the core Checkstyle product. We feel honored! Sadly, even though Checkstyle Addons is admittedly their inspiration, and we published 10 months ahead of them, the fact goes unmentioned. :-1:
In order to avoid name collisions with our original, we had to rename this check from RegexpOnFilename to RegexpOnFilenameOrg.

This check is useful for situations such as:

By default, this check flags leading and trailing spaces in file names.

The check works like this:

  1. Select the files to check. Only files which match the regular expression given in selection are checked. Leave blank to include all files.
  2. The expression given in 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.


selection regular expression
Limits the check to files whose canonical path name contains the given pattern. The canonical path is the simplest possible absolute path, including the file name (no .. elements etc.). unrestricted
regexp regular expression
The regular expression applied to the file name. ^(?:\s+.*|.*?\s+)$
mode Mode
whether regexp finds required or illegal matches. required means that all selected files must match the expression. illegal means that they must not. illegal
simple Boolean
If 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).

Custom Messages

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.

<module name="RegexpOnFilenameOrg">
  <message key="regexp.filepath.illegal" value="Filename ''{0}'' contains leading or trailing spaces."/>

To configure the check to ensure that Java files reside in Java source folders, not resource folders:

<module name="RegexpOnFilenameOrg">
  <property name="selection" value="\.java$"/>
  <property name="regexp" value="[\\/]src[\\/](?:test|main)[\\/]java[\\/]"/>
  <property name="mode" value="required"/>
  <property name="simple" value="false"/>
  <message key="regexp.filepath.required"
      value="The Java file ''{0}'' must reside in a Java source folder."/>

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_:

<module name="RegexpOnFilenameOrg">
  <property name="selection" value="[\\/]src[\\/]main[\\/]resources[\\/]html[\\/]views[\\/].+?\.html$"/>
  <property name="regexp" value="^view_.*"/>
  <property name="mode" value="required"/>
  <message key="regexp.filepath.required" value="Name of ''{0}'' must start with ''view_''."/>

To configure the check to ban GIF files in favor of PNG:

<module name="RegexpOnFilenameOrg">
  <property name="selection" value="(?i)\.gif$"/>
  <property name="regexp" value="."/>
  <message key="regexp.filepath.illegal" value="''{0}'' must be in PNG format, not GIF."/>

The (?i) at the start of the selection expression turns on case insensitivity, so that .gif, .GIF, or even .Gif are all matched.

Parent Module

Important: This check goes directly under Checker, not under TreeWalker.