Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-6035

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

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Replace this alternation with a character class.
    • Highlighting:
      Hide

      The alternation

      Show
      The alternation
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Covered Languages:
      Java
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources

      Description

      When an alternation contains multiple alternatives that consist of a single character, it can be rewritten as a character class. This should be preferred because it is more efficient and can even help prevent stack overflows when used inside a repetition (see rule S5998 ).

      Noncompliant Code Example

      Pattern.compile("a|b|c"); // Noncompliant
      

      Compliant Solution

      Pattern.compile("[abc]");
      // or
      Pattern.compile("[a-c]");
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              sebastian.hungerecker Sebastian Hungerecker
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: