Multiple spaces in a regular expression can make it hard to tell how many spaces should be matched. It’s more readable to use only one space and then indicate with a quantifier how many spaces are expected.
Pattern.compile("hello world");
Pattern.compile("hello {3}world");
In free-spacing mode (Pattern.COMMENTS
flag, or with embedded flag
expression (?x)
), whitespaces are ignored. In this case no issue should be triggered, because the whitespaces may be intended to improve
readability.