Home > Net >  How to reverse Regex comparison in BASH
How to reverse Regex comparison in BASH

Time:03-24

how can I reverse my regex comparison result

CodePudding user response:

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}$"
  • Related