0

Could anyone explain to me why this is returning TRUE? There are 7 z's yet it should be returning FALSE because I've set a max limit of 6 in the regex.

preg_match('/z{4,6}/', "zzzzzzz")

1 Answer 1

5

That is because your string includes a substring of 4 to 6 'z's. If you want the match to be against your whole string, you have to put in the anchors in your regex.

/^z{4,6}$/

or

/\Az{4,6}\z/
Sign up to request clarification or add additional context in comments.

1 Comment

@Chris: to clarify, ^ and $ are start and end of string respectively (or newline if using modifiers but lets not complicate things). So the regex here says "start of string, followed by 4 to 6 z's followed by end of string"

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.