简体   繁体   English

此字母数字字符串的正则表达式

[英]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 ? 为什么这个正则表达式与我的示例$string不匹配?

i need to check whether the given id is email address or ssmid.....also check there regular expression 我需要检查给定的id是电子邮件地址还是ssmid .....还检查那里的正则表达式

    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. 你错过了分隔符/你的正则表达式匹配至少3 S ,而不是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 . SS{2,}匹配字符串SSSSSS[...]的以下部分,因此至少为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 ). 此正则表达式恰好与SM匹配{2}次,然后与数字( \\d )匹配{4}次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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