首页 > 编程知识 正文

c语言正则表达式函数库(perl正则表达式匹配)

时间:2023-05-03 23:21:59 阅读:71740 作者:1177

/*检索字符串是正则表达式的主要应用。 在php中,它起作用并匹配perl样式的正则表达式

主要函数是preg_match (。 此函数的原型如下

intpreg_match(stringpattern,string subject[,array matches[,int flags]] )

在subject中搜索图案。 $matches[0]包含与整个模式匹配的文本。

$matches[1]包含与最初捕获的括号中的子模式匹配的文本。 这样类推

*/

$ text=' phpisthewebscriptinglanguageofchoice.';

在print ()文本/({$text}/)中搜索/'php/'/n ';

if (在preg _ match (/PHP/I )、$text () ) /模式分隔符之后的) I )表示搜索时不区分大小写

打印' -匹配/n/n ';

}else{

打印' -模式/n/n ';

}

$ text=' phpisthewebsitescriptinglanguageofchoice.';

在print ()文本/($text )/)中搜索单词web/)/n );

if (在preg _ match ((/bweb/b/I )、$text ) ) /模式分隔符之后的)/b )表示单词边界,因此只有独立单词“web”匹配

打印' -匹配/n/n ';

}else{

打印' -模式/n/n ';

}

在print ()文本/($text )/)中搜索单词web/)/n );

if(preg_match(/web/I )、$text ) ) }

打印' -匹配/n/n ';

}else{

打印' -模式/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 '; (?

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。