12

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 ?

3
  • 3
    For reference: "regex" is short for "regular expression". Making "regex expression" short for "regular expression expression".</pedantry> :) Commented Apr 17, 2012 at 2:42
  • 2
    _-+ is wrong. What does - do in a character class ([..])? Commented Apr 17, 2012 at 2:44
  • 2
    To further @pst's remark: The - in _-+ is the 17th character. Commented Apr 17, 2012 at 2:45

2 Answers 2

25

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I used /u in the end of my pattern when I get this Warning. Just try it. It means Ungreedy pattern

$pattern = "^([ `anything` ])$/u";

1 Comment

What question are you answering?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.