Search code examples
regexregex-group

Regex command to match combinations but not only uppercase letters


Is there a regex command to match all combinations of uppercase letters, lowercase, underscore, brackets, numbers, but not only Uppercase letter words or only numbers?

I thought i had it with this one:

(/\b(?![A-Z]+\b)(?![0-9]+\b)[a-zA-Z0-9_{}]+\b/)

That was until i encountered: ABC{hello}_HI_HelLo

This is not a match, and i would like my regex to match this string.

There seem to be something with the negative lookahead since it reads "ABC" and assumes it is a Uppercase letter word only so it does not match the string, only the part after the "{" is matched.

When you add an underscore after "ABC" you get a matching string: ABC_{hello}_HI_HelLo


Solution

  • There is a word boundary between _ and {

    You can assert a whitespace boundary to the left (?<!\S) and the right (?!\S) instead.

    The pattern matches:

    • (?<!\S) Assert a whitespace boundary to the left
    • (?![A-Z]+(?!\S)) Assert not only uppercase chars followed by a whitespace boundary at the right
    • (?![0-9]+(?!\S)) Assert not only digits followed by a whitespace boundary at the right
    • [a-zA-Z0-9_{}]+ Match 1 or more occurrences of any of the listed

    Regex demo