入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

php正则技巧,抓取、匹配中文、任意字符

创建时间:2015-10-31 投稿人: 浏览次数:6347

preg_match_all("/interface FastEthernet([sS][^!]*)/",$aaa,$cdpip) ;

interface FastEthernet 开始 中间任何字符 到 ! 终止


//--------------------------------------------------------------------------------------------------------------------------


 preg_match /u 验证中文时要增加/U 中文中{1,2}即1或是2个汉字

Warning:preg_match() [<a href="function.preg-match">function.preg-match</a>]: Compilation failed: PCRE does not support L, l, N, U, or u at offset 3

错误提示:

<b>Warning</b>:  preg_match() [<a href="function.preg-match">function.preg-match</a>]: Compilation failed: PCRE does not support L, l, N, U, or u at offset 3 in <b>D:XXX.php</b> on line <b>25</b><br />

错误原因:

preg_match("/^[u4e00-u9fa5]{1,4}$/",$str);
在使用上述代码匹配汉字时,出现如题错误。

测试1:

preg_match("/^[u4e00-u9fa5]{1,4}/",$str);
去掉结尾的“$”,错误依旧。

测试2:

preg_match("/^[x4e00-x9fa5]{1,4}/",$str);
将u改为“x”,错误消失,但是匹配失败。

测试3:

preg_match("/^[x{4e00}-x{9fa5}]{1,4}/",$str);
加上大括号,错误提示:

<b>Warning</b>:  preg_match() [<a href="function.preg-match">function.preg-match</a>]: Compilation failed: character value in x{...} sequence is too large at offset 9 in <b>D:XXX.php</b> on line <b>25</b><br />

测试4:

preg_match("/^[x{4e00}-x{9fa5}]{1,4}/u",$str);
加入参数u,错误消失,匹配正确。


//--------------------------------------------------------------------------------------------------------------------------


 想使用正则表达式来获取一段文本中的任意字符,写出如下匹配规则:
(.*)
结果运行之后才发现,无法获得换行之后的文本。
于是查了一下手册,才发现正则表达式中,“.”(点符号)匹配的是除了换行符“ ”以外的所有字符。
以下为正确的正则表达式匹配规则:
([sS]*)
同时,也可以用 “([dD]*)”、“([wW]*)” 来表示



声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。