Search code examples
regexsonarqube

RegExp duplicate range in character class


I'm wondering why /[\sA-Za-z0-9s#.\',-]/ is marked by my code quality linter as having duplicates?

I know a character class [abc] means match one of these, a, b or c.

\s = whitespace A-Z = match all caps a-z = match all lowercase 0-9 match all numbers s#.\',- = I really don't know...


Solution

  • The duplicate is coming from the s in s#.\',-, since s is included in a-z. Just remove it.

    /[\sA-Za-z0-9#.\',-]/
    

    s#.\',- matches any of those individual characters. Note that you don't need to escape ', unless this is embedded in a programming language string that uses ' as its delimiters.