【正则】Perl式正则 完整匹配单词 的正则怎么写(前瞻)
本帖最后由 default7 于 2014-10-27 09:30:18 编辑 使用Sublime或者很多编辑软件的搜索功能都会有这个功能,完整匹配单词,这个是如何实现的?
<br /><br />/**<br /> * @param $str<br /> *<br /> * @return string<br /> */<br />function getValue($str)<br />{<br /> return preg_match('/(?:\D)(\d{8})(?:\D)/', $str, $matches) ? $matches[1] : '';<br />}<br /><br />$arrStr = [<br /> '10208899', //有<br /> '#10208899', //有<br /> '#test^10208899', //有<br /> '#ff1020889900',//无<br /> '#0010208899',//无<br /> '#1020^10208899a',//有<br /> '#5566880&10208899f',//有<br /> 'test?#10208899',//有<br /> '#10208899.'//有<br />];<br /><br /><br />foreach ($arrStr as $i => $str) {<br /> $value = getValue($str);<br /> echo "$i\t{$str}\t\t=>\t{$value}\n";<br />}<br />
로그인 후 복사
以上匹配是错误的,0、1、2、7这些没有匹配出来。
<br />0 10208899 => <br />1 #10208899 => <br />2 #test^10208899 => <br />3 #ff1020889900 => <br />4 #0010208899 => <br />5 #1020^10208899a => 10208899<br />6 #5566880&10208899f => 10208899<br />7 test?#10208899 => <br />8 #10208899. => 10208899<br />
로그인 후 복사
------解决思路----------------------function getValue($str)<br />{<br /> return preg_match('/(?:^<br><font color='#FF8000'>------解决思路----------------------</font><br>\D)(\d{8})(?:\D<br><font color='#FF8000'>------解决思路----------------------</font><br>$)/', $str, $matches) ? $matches[1] : '';<br />}<br />
로그인 후 복사
0 10208899 => 10208899<br />1 #10208899 => 10208899<br />2 #test^10208899 => 10208899<br />3 #ff1020889900 => <br />4 #0010208899 => <br />5 #1020^10208899a => 10208899<br />6 #5566880&10208899f => 10208899<br />7 test?#10208899 => 10208899<br />8 #10208899. => 10208899<br /><br />
로그인 후 복사