Write efficient, error-free and safe regular expressions (regex) in Kotlin

Hello Kotlin developers,

After Java, PHP, JavaScript, and TypeScript developers, it’s your turn to get a set of rules to help you write efficient, error-free, and safe regular expressions (regex).

Here is the list of the new rules:

Bug Detection Rules:

  • S5856: Regular expressions should be syntactically valid
  • S5850: Alternatives in regular expressions should be grouped when used with anchors
  • S5868: Unicode Grapheme Clusters should be avoided inside regex character classes
  • S5842: Regex repetition pattern’s body should not match the empty String

Code Smell Detection Rules

  • S5846: Empty lines should not be tested with regex MULTILINE flag
  • S5869: Character classes in regular expressions should not contain the same character twice
  • S5843: Regular expressions should not be too complicated
  • S5857: Regular expressions character classes should be preferred over non-greedy quantifiers
  • S5867: Unicode-aware versions of character classes should be preferred

These features are available on SonarCloud and will be included in SonarQube 9.2 and SonarLint.

Alex