欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php知识点复习之正则表达式

程序员文章站 2024-01-14 15:27:40
...
上篇 http://www.BkJia.com/kf/201202/118458.html

//正则表达式的技术知识


// []定义字符集和example [a-z] [A-Z] [0-9] [\f\r\n\t]


//定位符^[A-Z][0-9]$开头结尾


//量词 * + ? {n} {m,n} {n.}指的都是重复次数


//选择|


//可打印字符:ASCII中33-127表示的字符,就是我们看得见的符号 空格,删除,回车,取消等等就典型的是不可打印字符


//POSOX正则表达式
//常用函数
echo "
";
$b4 = "abc";
$pattern = "[[:alpha:]]";
echo ereg($pattern,$b4);
//输出1
//eregi() 无符号的





$b7 = "asd&ass@ass&adfdf";

$pattern1 = "&";
$pattern2 = "[&@]";
print_r(split($pattern1,$b7));
print_r(split($pattern2,$b7));


echo "
";
$b8 = "helloworld";
$pattern3 = "world";
$replacement1 = "persion";
echo ereg_replace($pattern3,$replacement1,$b8);


echo "
";
echo sql_regcase("abcdefg");
//输出:[Aa][Bb][Cc][Dd][Ee][Ff][Gg]


//Perl 正则表达式perl是在支持posix的基础上额外的扩展\d数字\D非数字\s空白\S非空白\w字母数学符号下划线\W非字母数字符号下划线

//常用函数array preg_grep ( string pattern, array input )
echo "
";
$w1 = array("adad","adad4","asda","1asf3","sdfs");
$fl_array = preg_grep ("/^\D{1,}$/", $w1);//此处强调一点{1,}而不是posix中的{1.}
print_r($fl_array);


echo "
";
echo preg_match ("/a/","abc");
//输出1


echo "
";
//\b表示单词的边界
if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}


if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
//尤其是在匹配电子邮箱是常用


echo "
";
$pattern5 = "/[\*]+/";
$w3 = "aaa***bbb*ccc";
print_r(preg_split($pattern5,$w3));


echo "
";
$pattern6 = "/world/";
$content2 = "girl";
$tre = "hello world";
echo preg_replace($pattern6,$content2,$tre);


//string preg_quote ( string str [, string delimiter] )转义
echo "
";
$keywords = "$40 for a g3/400";
$keywords = preg_quote ($keywords, "/");//此处的/若是不加,就不会转义g3/400中的/;
echo $keywords;

摘自 kaituozhe345的专栏