简体   繁体   中英

Regular expression for this alphanumeric string

$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";
}

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)

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM