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 overflow the stack

        intentionality - complete
        reliability
        Bug
        • regex

        Why is this an issue?

        The Java regex engine uses recursive method calls to implement backtracking. Therefore when a repetition inside a regular expression contains multiple paths (i.e. the body of the repetition contains an alternation (|), an optional element or another repetition), trying to match the regular expression can cause a stack overflow on large inputs. This does not happen when using a possessive quantifier (such as *+ instead of *) or when using a character class inside a repetition (e.g. [ab]* instead of (a|b)*).

        The size of the input required to overflow the stack depends on various factors, including of course the stack size of the JVM. One thing that significantly increases the size of the input that can be processed is if each iteration of the repetition goes through a chain of multiple constant characters because such consecutive characters will be matched by the regex engine without invoking any recursion.

        For example, on a JVM with a stack size of 1MB, the regex (?:a|b)* will overflow the stack after matching around 6000 characters (actual numbers may differ between JVM versions and even across multiple runs on the same JVM) whereas (?:abc|def)* can handle around 15000 characters.

        Since often times stack growth can’t easily be avoided, this rule will only report issues on regular expressions if they can cause a stack overflow on realistically sized inputs. You can adjust the maxStackConsumptionFactor parameter to adjust this.

        Noncompliant code example

        Pattern.compile("(a|b)*"); // Noncompliant
        Pattern.compile("(.|\n)*"); // Noncompliant
        Pattern.compile("(ab?)*"); // Noncompliant
        

        Compliant solution

        Pattern.compile("[ab]*"); // Character classes don't cause recursion the way that '|' does
        Pattern.compile("(?s).*"); // Enabling the (?s) flag makes '.' match line breaks, so '|\n' isn't necessary
        Pattern.compile("(ab?)*+"); // Possessive quantifiers don't cause recursion because they disable backtracking
        
          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