Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="header"
            value="a\nb"/>
        <property name="fileExtensions" value="java"/>
    </module>
</module>

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

    /*ab*/
    bla bla
    

    but not

    /*a
    cb*/
    bla bla
    
  • adding plain newlines in the checkstyle XML file
  • adding &#10; or \x0A for newline characters

value="/\*a&#10;cb\*/\n" matches

/*a
cb*/
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 https://github.com/krichter722/maven-checkstyle-plugin-multiline in order to facilitate investigating.

like image 306
Kalle Richter Avatar asked Oct 21 '15 18:10

Kalle Richter


1 Answers

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

(?m)a($\s+^c)?b

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

like image 198
Bohemian Avatar answered Oct 12 '22 09:10

Bohemian