Home > Blockchain >  How to remove char from regex
How to remove char from regex

Time:09-27

I'm not got, yet, with regex. I've been trying to break my head to get this to work.

I need a regex that allows the user to enter

  • Any alphabetical char (a-z)
  • Any number
  • For special char only "-" and "_".
  • "@" is not allowed.

I got this but no dice. [^a-zA-Z0-9]

Thanks

CodePudding user response:

^[\w-] $

will match a string following the rules you describe. \w matches letters, digits, or underscore, then it adds - to that set. Anchoring with ^ and $ requires all the characters in the string to match this pattern.

CodePudding user response:

remove ^ character in square brackets because is negative ranges, add some \-\_ to allow '-' and '_' character inside square brackets

[a-zA-Z0-9\-\_] 
  • Related