Regular expression that accepts letters, numbers and underline

Asked

Viewed 749 times

3

How do I make the ER below accept letters, numbers and underline, and continue not allowing the other characters?

preg_match('/[^a-z\d]/', $_POST['login'])

1 answer

2

The clasps ([]) define a character class, that is, accepts whatever is inside them. Ex: [ab] means "the letter a or the letter b".

But when the first character inside the brackets is a ^, you will be denying what is inside it. Ex: [^ab] means "anything that is not a nor b".

So in doing [^a-z\d], you are rejecting the lower case letters and digits. To accept letters, numbers and underline, you must include them in the brackets and remove the ^, then I would be [A-Za-z0-9_].

I’m assuming you want to accept strings with more than one character, so use the quantifier +, meaning "one or more occurrences". Then the expression is [A-Za-z0-9_]+.

Since these are the only characters allowed, use also ^ out of of the brackets and at the beginning of the expression, because it means beginning of the string, and at the end put $, which means the end of the string. The complete expression is:

preg_match("/^[A-Za-z0-9_]+$/", $_POST['login'])

That is: one or more occurrences (+) letters, numbers or underline ([A-Za-z0-9_]), from the beginning (^) at the end ($) string.

  • \w = A-Za-Z0-9_

  • @Leocaracciolo \w can include other characters if you have the option u (Unicode) enabled: https://ideone.com/GPumF9 - It’s okay that in this specific case it wouldn’t matter, because I didn’t use this option, but anyway thank you for reminding me that the \w exists (sometimes I forget these shortcuts) - another thing is that I don’t remember if in PHP the locale or other environmental settings may also influence the behavior of \w (I guess that’s why I end up not using it most of the time, because I never know the exact rules in every language).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.