Write regexp in Java running fast, with the correct amount of resources and really doing what you intended

Hello Java developers,

Back in July 2020, we released our first set of rules helping you to write efficient, error-free and safe regular expressions ( regex ) in Java. It’s time to finish the year 2020 with a last present: a second set of rules that this time will help your write regexp running fast, with the correct amount of resources and really doing what you intended. I know it’s ambitious but with the technology we put in place, we really believe in these rules and you would no longer want to write regexp without them!

Here is the list of the rules dedicated to regexp that were just deployed on SonarCloud:

  • S5994: Regex patterns following a possessive quantifier should not always fail
  • S5996: Regex boundaries should not be used in a way that can never match
  • S5998: Regular expressions should not overflow the stack
  • S6001: Back references in regular expressions should only refer to capturing groups that are matched before the reference
  • S6002: Regex lookahead assertions should not be contradictory
  • S5855: Regex alternatives should not be redundant
  • S6019: Reluctant quantifiers in regular expressions should be followed by an expression that can’t match the empty string
  • S6035: Single-character alternations in regular expressions should be replaced with character classes
  • S6070: The regex escape sequence \cX should only be used with characters in the @-_ range

You can find all the Java rules dedicated to regexp here.

These features are already available on SonarCloud, and will be included in SonarQube 8.7.


1 Like