Hi I have a problem with my regex pattern:
preg_match_all('/!!\d{3}/', '!!333!!333 !!333 test', $result);
I want this to match !!333 but not !!333!333. How can I modify this regex to match only a max length of 5 characters – two ! and three numbers.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I find the easiest and most descriptive way to do this is with negative lookaheads and lookbehinds.
See:
This says: match anything of the form !![0-9][0-9][0-9] which doesn’t have anything other than a space in front or behind it. Note that these lookaheads/lookbehinds aren’t matched themselves, they are “zero-width assertions”, they are thrown away and so you only get “!!333″ etc in your match, not ” !!333″ etc.
It returns
Also
returns
That is, all but the last two which are too long.
See Lookahead tutorial.