How to specify an optional regex group with a newline character in Checkstyle's RegexpHeader?


I'm having trouble specifying a regex group (()?) containing a newline character (\n) in the RegexpHeader module of the maven-checkstyle-plugin, e.g.

<module name="Checker">
    <module name="RegexpHeader">
        <property name="fileExtensions" value="java"/>

where as value="a(\nc)?b" fails with error Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:2.17:check (check_style) on project document-scanner: Failed during checkstyle configuration: cannot initialize module RegexpHeader - Cannot set property 'header' to 'a(\nc)?b' in module RegexpHeader: InvocationTargetException: line 1 in header specification is not a regular expression -> [Help 1].

According to the RegexpHeader docs

Individual header lines must be separated by the string "\n"

Which explains that the regex causes an error, yet it prevents specification of regular expressions with optional groups containing a newline character. Examples for this case are also not specified.

Solutions which don't work:

  • Replacing \n with \\n and \\\n doesn't work (causes the same error as above).
  • value="/*a(\x10c)?b*/\n" matches a file starting with

    bla bla

    but not

    bla bla
  • adding plain newlines in the checkstyle XML file
  • adding or \x0A for newline characters


bla bla

so it seem to be the regex control characters (()?) which are causing trouble.

I'm using the maven-checkstyle-plugin 2.17 and Java 7.

I created in order to facilitate investigating.

Try a multiline mode where ^ and $ match start/end of each line:


You may have to experiment a bit with how to code the backslash.