php perl正则,【正则】Perl式正则 完整匹配单词 的正则如何写(前瞻)

本帖最后由 default7 于 2014-10-27 09:30:18 编辑 使用Sublime或者很多编辑软件的搜索功能都会有这个功能,完整匹配单词,这个是如何实现的?

11563580.png

/**

* @param $str

*

* @return string

*/

function getValue($str)

{

return preg_match('/(?:\D)(\d{8})(?:\D)/', $str, $matches) ? $matches[1] : '';

}

$arrStr = [

'10208899', //有

'#10208899', //有

'#test^10208899', //有

'#ff1020889900',//无

'#0010208899',//无

'#1020^10208899a',//有

'#5566880&10208899f',//有

'test?#10208899',//有

'#10208899.'//有

];

foreach ($arrStr as $i => $str) {

$value = getValue($str);

echo "$i\t{$str}\t\t=>\t{$value}\n";

}

以上匹配是错误的,0、1、2、7这些没有匹配出来。

010208899=>

1#10208899=>

2#test^10208899=>

3#ff1020889900=>

4#0010208899=>

5#1020^10208899a=>10208899

6#5566880&10208899f=>10208899

7test?#10208899=>

8#10208899.=>10208899

------解决思路----------------------

function getValue($str)

{

return preg_match('/(?:^

------解决思路----------------------

\D)(\d{8})(?:\D

------解决思路----------------------

$)/', $str, $matches) ? $matches[1] : '';

}010208899=>10208899

1#10208899=>10208899

2#test^10208899=>10208899

3#ff1020889900=>

4#0010208899=>

5#1020^10208899a=>10208899

6#5566880&10208899f=>10208899

7test?#10208899=>10208899

8#10208899.=>10208899

相关文章

相关视频

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值