SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
Java

Java static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your JAVA code

  • All rules 733
  • Vulnerability60
  • Bug175
  • Security Hotspot40
  • Code Smell458

  • Quick Fix 65
Filtered: 30 rules found
regex
    Impact
      Clean code attribute
        1. Character classes in regular expressions should not contain only one character

           Code Smell
        2. Superfluous curly brace quantifiers should be avoided

           Code Smell
        3. Non-capturing groups without quantifier should not be used

           Code Smell
        4. Regular expression quantifiers and character classes should be used concisely

           Code Smell
        5. Regular expressions should not contain empty groups

           Code Smell
        6. Regular expressions should not contain multiple spaces

           Code Smell
        7. The regex escape sequence \cX should only be used with characters in the @-_ range

           Bug
        8. Single-character alternations in regular expressions should be replaced with character classes

           Code Smell
        9. Reluctant quantifiers in regular expressions should be followed by an expression that can't match the empty string

           Code Smell
        10. Regex lookahead assertions should not be contradictory

           Bug
        11. Back references in regular expressions should only refer to capturing groups that are matched before the reference

           Bug
        12. Regular expressions should not overflow the stack

           Bug
        13. Regex boundaries should not be used in a way that can never be matched

           Bug
        14. Regex patterns following a possessive quantifier should not always fail

           Bug
        15. Character classes in regular expressions should not contain the same character twice

           Code Smell
        16. Unicode Grapheme Clusters should be avoided inside regex character classes

           Bug
        17. Unicode-aware versions of character classes should be preferred

           Code Smell
        18. Case insensitive Unicode regular expressions should enable the "UNICODE_CASE" flag

           Bug
        19. Names of regular expressions named groups should be used

           Code Smell
        20. Character classes should be preferred over reluctant quantifiers in regular expressions

           Code Smell
        21. Regular expressions should be syntactically valid

           Bug
        22. Regex alternatives should not be redundant

           Bug
        23. Regexes containing characters subject to normalization should use the CANON_EQ flag

           Code Smell
        24. Using slow regular expressions is security-sensitive

           Security Hotspot
        25. Alternatives in regular expressions should be grouped when used with anchors

           Bug
        26. Empty lines should not be tested with regex MULTILINE flag

           Code Smell
        27. Regular expressions should not be too complicated

           Code Smell
        28. Repeated patterns in regular expressions should not match the empty string

           Bug
        29. "String#replace" should be preferred to "String#replaceAll"

           Code Smell
        30. Regex patterns should not be created needlessly

           Code Smell

        Regular expressions should not be too complicated

        intentionality - clear
        maintainability
        Code Smell
        • regex

        Why is this an issue?

        Overly complicated regular expressions are hard to read and to maintain and can easily cause hard-to-find bugs. If a regex is too complicated, you should consider replacing it or parts of it with regular code or splitting it apart into multiple patterns at least.

        The complexity of a regular expression is determined as follows:

        Each of the following operators increases the complexity by an amount equal to the current nesting level and also increases the current nesting level by one for its arguments:

        • | - when multiple | operators are used together, the subsequent ones only increase the complexity by 1
        • && (inside character classes) - when multiple && operators are used together, the subsequent ones only increase the complexity by 1
        • Quantifiers (*, +, ?, {n,m}, {n,} or {n})
        • Non-capturing groups that set flags (such as (?i:some_pattern) or (?i)some_pattern)
        • Lookahead and lookbehind assertions

        Additionally, each use of the following features increase the complexity by 1 regardless of nesting:

        • character classes
        • back references

        If a regular expression is split among multiple variables, the complexity is calculated for each variable individually, not for the whole regular expression. If a regular expression is split over multiple lines, each line is treated individually if it is accompanied by a comment (either a Java comment or a comment within the regular expression), otherwise the regular expression is analyzed as a whole.

        Noncompliant code example

        if (dateString.matches("^(?:(?:31(\\/|-|\\.)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.)(?:0?[13-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$")) {
            handleDate(dateString);
        }
        

        Compliant solution

        if (dateString.matches("^\\d{1,2}([-/.])\\d{1,2}\\1\\d{1,4}$")) {
            String dateParts[] = dateString.split("[-/.]");
            int day = Integer.parseInt(dateParts[0]);
            int month = Integer.parseInt(dateParts[1]);
            int year = Integer.parseInt(dateParts[2]);
            // Put logic to validate and process the date based on its integer parts here
        }
        

        Exceptions

        Regular expressions are only analyzed if all parts of the regular expression are either string literals, effectively final local variables or static final fields, all of which can be combined using the '+' operator.

        When a regular expression is split among multiple variables or commented lines, each part is only analyzed if it is syntactically valid by itself.

          Available In:
        • SonarQube IdeCatch issues on the fly,
          in your IDE
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube Community BuildAnalyze code in your
          on-premise CI
          Available Since
          9.1
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use