Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"range out of order in character class" in PHP regex?

Tags:

regex

php

I am trying to get this work for a while but in vain. I want to create a php regex to check if a string has atleast one number and atleast one of the symbols amongst ( _-+=*& )

This is my regex

 $result = preg_match('/^(?=.*\d)(?=.*[_-+=*&]).{3,}$/',$pass);

I get the following error Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 17 in myfile.php on line 8

any help ?

like image 586
zed Avatar asked Apr 17 '12 02:04

zed


1 Answers

The - needs to be escaped, or placed at the start / end of the [...] list:

$result = preg_match('/^(?=.*\d)(?=.*[-_+=*&]).{3,}$/',$pass);

If you don't, - is interpreted as the range operator and if x > y in [x-y] you get that error.

like image 124
Alix Axel Avatar answered Oct 01 '22 23:10

Alix Axel