Ichos Dichos
Ichos Dichos

Reputation: 51

Regex, match characters and doesn't contain more than one _ followed by another _

I already got the first part with the matching characters, which looks as following: ^[A-Za-z0-9_]{0,}$
Now I need too check if there is any underscores followed by another underscore. How can I achieve this?

List for explanation:
AAA_A - True
A_AA_A - True
A__AA - False

Upvotes: 1

Views: 132

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627100

Move the _ outside of the character class and use a * quantified group:

^[A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$

See the regex demo. Due to the * after the group, the expression will still match empty strings, but the + will not let the whole string be equal to 1 underscore. That also means that _ can appear at the start of the string (add a (?!^) before _ if you need to disallow this).

If the string can be empty, or can be equal to _, you may also use the ^[A-Za-z0-9]*(?:_(?!_)[A-Za-z0-9]*)*$ (demo) regex variation.

Pattern details.

  • ^ - start of string
  • [A-Za-z0-9]* - zero or more alphanumerics
  • (?: - start of a group matching a sequence of:
    • _ - underscore
    • [A-Za-z0-9]+ - one or more alphanumerics
  • )* - end of the group, can occur 0 or more times
  • $ - end of string.

Another approach - using a negative lookahead:

^(?!.*__)[A-Za-z0-9_]*$

See this regex demo. The (?!.*__) will fail the match once there are __ found somewhere ahead after the start of string. To analyze a multiline string, use a DOTALL modifier (in Ruby/Oniguruma, /m modifier), and if it is not available (like in JS), use [\s\S]* instead of .*.

Upvotes: 1

Related Questions