Skip to content Skip to sidebar Skip to footer

Regex To Include Alphanumeric And _

I'm trying to create a regular expression to match alphanumeric characters and the underscore _. This is my regex: '\w_*[^-$\s\]' and my impression is that this regex means any alp

Solution 1:

Regular expressions are read as patterns which actually match characters in a string, left to right, so your pattern actually matches an alphanumeric, THEN an underscore (0 or more), THEN at least one character that is not a hyphen, dollar, or whitespace.

Since you're trying to alternate on character types, just use a character class to show what characters you're allowing:

[\w_]

This checks that ANY part of the string matches it, so let's anchor it to the beginning and and of the string:

^[\w_]$

And now we see that the character class lacks a quantifier, so we are matching on exactly ONE character. We can fix that using + (if you want one or more characters, no empty strings) or * (if you want to allow empty strings). I'll use + here.

^[\w_]+$

As it turns out, the \w character class already includes the underscore, so we can remove the redundant underscore from the pattern:

^[\w]+$

And now we have only one character in the character class, so we no longer need the character class brackets at all:

^\w+$

And that's all you need, unless I'm missing something about your requirements.

Solution 2:

Yes, you are semi-correct if the closing bracket was not escaped and you edited your regex a bit. Also the token \w matches underscore, so you do not need to repeat this character. Your regular expression says:

\w         # word characters (a-z, A-Z, 0-9, _)
_*         # '_' (0or more times)
[^-$\s]    # anycharacterexcept: '-', '$', whitespace (\n, \r, \t, \f, and " ")

You could simply write your entire regex as follows to match word characters:

\w+        # word characters ( a-z, A-Z, 0-9, _ ) (1 or more times)

If you want to match an entire string, be sure to anchor your expression.

^\w+$

Explanation:

^          # the beginning of the string
 \w+       #   word characters ( a-z, A-Z, 0-9, _ ) (1 or more times)$ # before an optional \n, and the end of the string

Post a Comment for "Regex To Include Alphanumeric And _"