Why it does not count _ as symbol?

Hey there!

I have this preg_match() to check if there are symbols in password

if(!preg_match('/[^0-9a-zA-z]/', $password)){echo 'There are no symbols';}

but if user makes password like RAndom_33 it says there are no symbol till now other symbols worked!

Why could this happen?

You need to add _ at the end of your regex.

Like '/[^0-9a-zA-z_]/' ?

Yes. Like that.

Didn’t you try this?

@spaceshiptrooper @chorn Sorry for late replay! It seems that it still says that symbols was not used :frowning:

[A-z] is a weird range 


Not in isolation, but used in combination with a-z is weird. I guess it’s a typo.

Trying things out on an on-line regex tester, I’m having a hard time getting it to recognise underscores. Not sure why, I wasn’t aware of anything special about them in regex.

I would prefer \W for that anyways.

1 Like

If you are using other characters too, you have to put them in there too. If you don’t, it’ll say it isn’t in there.

I think it’s precisely because the range is A-z that the underscore isn’t being considered a “symbol”.

The ASCII characters A-Z range from decimal 65-90. The ASCII characters a-z range from decimal 97-122. The characters `^_`` range from decimal 91-96.

In other words the underscore falls with the range A-z

4 Likes

That would explain the problem I was having, if I copied the regex, but had not noticed the typo.

But I just found that \W considers an underscore a word character, so maybe the 1-9a-zA-Z would be better and the problem was the typo all along.

1 Like

The only potential problem with the “word character” escape is it’s based on locale. So for example the â€œĂ±â€ in señor wouldn’t match an “n”.

But only when used with the u (PCRE_UTF8) modifier.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.