简体   繁体   English

正则表达式检查字符串是否包含

[英]Regular expression check if string contain

I need a regular expression. 我需要一个正则表达式。 In order to check if a given string contains only [az][AZ][0-9] and a set of this char: .,|$_ . 为了检查给定的字符串是否仅包含[az][AZ][0-9]和以下字符集: .,|$_

In addition I need also a regex to check if the string contains at least one char for each of these 4 groups. 另外,我还需要一个正则表达式来检查字符串是否对这4个组中的每个组至少包含一个字符。

I'm working on PHP but I think regex are generic. 我正在使用PHP,但是我认为regex是通用的。

Look at the code. 看代码。 and try this. 尝试一下

$var = "stackoverflow";

if(preg_match('/[A-Z]|[a-z]|[0-9]|[.,$_]/', $var)) {
    echo "Matched";
}
else {
    echo "do not match";
}

That's it. 而已。

Thanks. 谢谢。

$var = "stackoverflow";

if( (preg_match('/[A-Z]{1}/', $var) &&
     preg_match('/[a-z]{1}/', $var) &&
     preg_match('/[0-9]{1}/', $var) &&
     preg_match('/[.,$_]{1}/', $var)) ) {
    echo "String matches and contains at least one character from each group.";
}
elseif(preg_match('/[A-Z]|[a-z]|[0-9]|[.,$_]/', $var)) {
    echo "String matches, but doesn't contain characters from all four groups.";
}
else {
    echo "String does not match.";
}

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

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