php perl风格 正则表达式
2010-10-26 12:44
309 查看
/* 查找字符串是正则表达式的主要应用。在php中,可以作用的并且用于匹配perl风格正则表达式的
主要函数是 preg_match()。该函数原型如下
int preg_match(string pattern, string subject[,array matches[,int flags]])
在subject中搜索pattern。$matches[0]将包含与整个模式匹配的文本,
$matches[1]将包含与第一个捕获的括号中的子模式所匹配的文本。依此类推
*/
$text = "PHP is the web scripting language of choice.";
print("在文本/"{$text}/" 中搜索 /"php/"/n");
if (preg_match("/php/i", $text)){ //模式定界符后面的"i"表示搜索时不区分大水写
print "->找到一个匹配/n/n";
}else{
print "->未找到模式/n/n";
}
$text = "PHP is the website scripting language of choice.";
print("在文本/"{$text}/" 中搜索单词 /"web/"/n");
if(preg_match("//bweb/b/i", $text)){ //模式定界符后面的"/b"表示单词的边界,因此只有独立的单词"web"才会匹配
print "->找到一个匹配/n/n";
}else{
print "->未找到模式/n/n";
}
print("在文本/"{$text}/" 中搜索单词 /"web/"/n");
if(preg_match("/web/i", $text)){
print "->找到一个匹配/n/n";
}else{
print "->未找到模式/n/n";
}
$text = "http://www.php.net/index.html";
print("从UR/"{$text}/" 中取得主机名和域名/n");
if(preg_match("/^(http:////)?([^//]+)/i", $text, $matches)){ //模式中圆括号括起来的为子表达式
$host = $matches[2];
print "->主机名为:$host/n/n";
preg_match("/[^/.//]+/.[^/.//]+$/",$host, $matches); //从主机名中取得后面两段
print "->域名为:{$matches[0]}/n";
}
?>
主要函数是 preg_match()。该函数原型如下
int preg_match(string pattern, string subject[,array matches[,int flags]])
在subject中搜索pattern。$matches[0]将包含与整个模式匹配的文本,
$matches[1]将包含与第一个捕获的括号中的子模式所匹配的文本。依此类推
*/
$text = "PHP is the web scripting language of choice.";
print("在文本/"{$text}/" 中搜索 /"php/"/n");
if (preg_match("/php/i", $text)){ //模式定界符后面的"i"表示搜索时不区分大水写
print "->找到一个匹配/n/n";
}else{
print "->未找到模式/n/n";
}
$text = "PHP is the website scripting language of choice.";
print("在文本/"{$text}/" 中搜索单词 /"web/"/n");
if(preg_match("//bweb/b/i", $text)){ //模式定界符后面的"/b"表示单词的边界,因此只有独立的单词"web"才会匹配
print "->找到一个匹配/n/n";
}else{
print "->未找到模式/n/n";
}
print("在文本/"{$text}/" 中搜索单词 /"web/"/n");
if(preg_match("/web/i", $text)){
print "->找到一个匹配/n/n";
}else{
print "->未找到模式/n/n";
}
$text = "http://www.php.net/index.html";
print("从UR/"{$text}/" 中取得主机名和域名/n");
if(preg_match("/^(http:////)?([^//]+)/i", $text, $matches)){ //模式中圆括号括起来的为子表达式
$host = $matches[2];
print "->主机名为:$host/n/n";
preg_match("/[^/.//]+/.[^/.//]+$/",$host, $matches); //从主机名中取得后面两段
print "->域名为:{$matches[0]}/n";
}
?>
相关文章推荐
- PHP -- Perl风格正则表达式
- 在PHP中使用与Perl兼容的正则表达式
- 解析posix与perl标准的正则表达式区别 ---PHP
- 在PHP中使用与Perl兼容的正则表达式
- POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
- PHP中Perl兼容类型的正则表达式相关函数总结
- POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)
- 在PHP中使用与Perl兼容的正则表达式
- 在PHP中使用与Perl兼容的正则表达式
- 正则表达式使用详解 (php,perl,unix,javascript) (有点意思)
- java 风格的正则表达式 vs Perl风格的正则表达式
- 11.2、正则表达式Perl风格函数的应用
- PHP与Perl兼容的正则表达式
- 在PHP中使用与Perl兼容的正则表达式
- 在PHP里面运用与Perl兼容地正则表达式【转载】
- PHP学习笔记:PERL兼容正则表达式
- POSIX风格正则表达式和Perl风格正则表达式(9元包邮)
- PHP中基于perl的正则表达式处理函数
- PHP中正则表达式函数(Perl兼容)
- POSIX 风格和兼容 Perl 风格两种正则表达式主要函数的类比(preg_match, preg_replace, ereg, ereg_replace)