Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exclude underscore from alpha numeric regex

Tags:

regex

I want use \w regex for to allow alpha numeric but I don't want underscore _ to be part of it. Since _ is included in \w. So I have coded like this but doesn't work, what is my mistake?

(/^roger\w{2,3}[0-9a-z]/i)

I am expecting any character other than A-Z or 1-2 to be exclude

ex - roger3_2 or roger46_ or roger2_

but

roger54 or roger4a or roger455 or rogerAAA

are to be ok

like image 914
raindrop Avatar asked Mar 28 '12 15:03

raindrop


People also ask

Does \w include _?

A domain name may include lowercase and uppercase letters, numbers, period signs and dashes, but no underscores. \w includes all of the above, plus an underscore.

Does regex \W include underscore?

\W matches any character that's not a letter, digit, or underscore. It prevents the regex from matching characters before or after the phrase.

Is underscore a special character in regex?

Regex doesn't recognize underscore as special character.

What is Alnum regex?

A regular expression for an alphanumeric string checks that the string contains lowercase letters a-z , uppercase letters A-Z , and numbers 0-9 .


2 Answers

You could try something like:

[^_\W]+
like image 112
Bogdan Emil Mariesan Avatar answered Oct 27 '22 19:10

Bogdan Emil Mariesan


  • A numeric code point is \pN or \p{Number}.
  • A digit code point is \d, \p{digit}, \p{Nd}, \p{Decimal_Number}, or \p{Numeric_Type=Decimal}.
  • An alphabetic code point is \p{alpha} or \p{Alphabetic}. It includes all \p{Digit}, \p{Letter}, and \p{Letter_Number} code points, as well as certain \p{Mark} and \p{Symbol} code points.
  • A programming-word code point is \w, or [\p{Alphabetic}\p{Digit}\p{Mark}\p{Connector_Punctuation}].

An alphanumeric code point by the strictest definition is consequently and necessarily [\p{Alphabetic}\p{Number}], typically abbreviated [\p{alpha}\pN].

like image 30
tchrist Avatar answered Oct 27 '22 19:10

tchrist