'How to reverse Regex comparison in BASH

how can I reverse my regex comparison result



Solution 1:[1]

The problem is that the regular expression is invalid. Specifically, the bracket expression ([a-z0-9-_]) is invalid because it contains a dash in the wrong place.

To include a literal dash character in a bracket expression, you need to put it at the beginning of the expression ([-otherchars]), at the end ([otherchars-]), or at at the end of a range ([*--] will match "*", "+", ",", and "-" in the C locale). Your bracket expression includes 0-9-_, which looks like two ranges stuck together, and is invalid.

To fix it, just move the "-" to the beginning or end of the bracket expression:

USERNAME_REGEX="^[a-z0-9_-]{2,15}$"    # or
USERNAME_REGEX="^[-a-z0-9_]{2,15}$"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gordon Davisson