1 .方括号[]
表示在止标字符串中寻找字符。
2.连字符(-)
例如:[a-z]
3.点字符(.)
是一个通配符,代表所有字符和数字
4.限定符(+*?{n,m})
+表示其前面字符少一个
*表示期前面字符不止一个或零个。如"y*",表示目标字符串包含零或不止一个y.
5.行定位符(^和$)
^表示目标字符串在开头出现,$表示目标字符串在结尾出现
6.排除字符([^])
7.括号字符[()]
表示字符串是一个整体
8.选择字符(|)
9.转义字符(\)与反斜线(\)
匹配函数:preg_match(正则表达式,目标字符串,[数组])
替换字符串函数:preg_replace(正则表达式规范,欲取代字符串子串,目标字符串,[替换的个数])
如:preg_replace(‘/\s/‘,‘-‘,$aa)
切分字符串:strtok(正则表达式,目标字符串)
如:strtok($string," ")
举例:
<?php
$email="wangxiaoming2011@hotmail.com";
$email2="The email is liuxiaoshuai_2011@hotmail.com";
$email3=". @hotmail.com";
$asemail="this is wangxiaoming2011@hotmail.com";
$regx=‘/^[a-zA-Z0-9_.]+@[a-zA-Z0-9_]+\.[a-zA-Z0-9_.]+$/‘;
$regx2=‘/[a-zA-Z0-9_.]+@[a-zA-Z0-9_]+\.[a-zA-Z0-9_.]+$/‘;
if(preg_match($regx, $email,$a)){
echo "This is an email.";
print_r($a);
};
echo "</br>";
if(preg_match($regx2, $email2,$b)){
echo "This is an email.";
print_r($b);
}
echo "</br>";
if(preg_match($regx, $asemail,$c)){
echo "This is an email.";
}else{
echo "This is not an email.";
print_r($c);
}
echo "</br>";
if(preg_match($regx, $email3,$d)){
echo "This is an email.";
}else{
echo "This is not an email.";
print_r($c);
}
echo "</br>";
?>
.
原文:https://www.cnblogs.com/soft2408/p/10970738.html