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

        Empty lines should not be tested with regex MULTILINE flag

        intentionality - logical
        maintainability
        Code Smell
        • regex

        Why is this an issue?

        One way to test for empty lines is to use the regex "^$", which can be extremely handy when filtering out empty lines from collections of Strings, for instance. With regard to this, the Javadoc for Pattern (Line Terminators) states the following:

        By default, the regular expressions ^ and $ ignore line terminators and only match at the beginning and the end, respectively, of the entire input sequence. If MULTILINE mode is activated then ^ matches at the beginning of input and after any line terminator except at the end of input. When in MULTILINE mode $ matches just before a line terminator or the end of the input sequence.

        As emphasized, ^ is not going to match at the end of an input, and the end of the input is necessarily included in the empty string, which might lead to completely missing empty lines, while it would be the initial reason for using such regex.

        Therefore, when searching for empty lines using a multi-line regular expression, you should also check whether the string is empty.

        This rule is raising an issue every time a pattern that can match the empty string is used with MULTILINE flag and without calling isEmpty() on the string.

        Noncompliant code example

        static final Pattern p = Pattern.compile("^$", Pattern.MULTILINE); // Noncompliant
        
        // Alternatively
        static final Pattern p = Pattern.compile("(?m)^$"); // Noncompliant
        
        
        boolean containsEmptyLines(String str) {
            return p.matcher(str).find();
        }
        
        // ...
        System.out.println(containsEmptyLines("a\n\nb")); // correctly prints 'true'
        System.out.println(containsEmptyLines("")); // incorrectly prints 'false'
        

        Compliant solution

        static final Pattern p = Pattern.compile("^$", Pattern.MULTILINE);
        
        boolean containsEmptyLines(String str) {
            return p.matcher(str).find() || str.isEmpty();
        }
        
        // ...
        System.out.println(containsEmptyLines("a\n\nb")); // correctly prints 'true'
        System.out.println(containsEmptyLines("")); // also correctly prints 'true'
        
          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