0
$string = 'SSM1234';
preg_match("SS{2,}\M+\[0-9]", $string);

Why does this regex not match my sample $string?

i need to check whether the given id is email address or ssmid.....also check there regular expression

    if((!preg_match("/^S{2,}M+[0-9]+$/", $Forgot_field)) || (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $Forgot_field))){

                $result = "Enter a valid SSM ID or Email ID";
}
1
  • It would be helpful if you included some other sample strings that you want to match. Do all matching strings start with "SSM" or can they have any number of S's or M's (or any letter)? Commented Nov 28, 2013 at 8:27

2 Answers 2

1

This should works

$string = 'SSM1234'; 
$res = preg_match('/^S{2,}M+[0-9]+$/', $string);

You missed the delimiter / and your regexp match at least 3 S, not 2.

I've added also the start (^) and the end ($) of match. But it's no mandatory (it depends by your case)

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

Comments

1

This SS{2,} matches the following part of a string SSS or SSS[...] so at least 3 S.

What you need is:

/^S{2,}M[0-9]+$/

Of course with the missing delimiters, as mentioned abouth.

This regex matches strings like:

SSM1
SSSM1
SSSSSSM157453247
SSSM123

and so on.

If you need a regex which strictly matches your sample string try:

/^S{2}M\d{4}$/

This regex matches exactly {2} times a S, a M and then {4} times a digit (\d).

Comments

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.