Home  >  Article  >  Backend Development  >  正则匹配手机号后四位至少有一个是8?如何写?

正则匹配手机号后四位至少有一个是8?如何写?

WBOY
WBOYOriginal
2016-06-06 20:32:311675browse

我有一串手机号:'13141078884 13161143334 13263362224 18511925554 18612623450 13141102288 13161146662 13263370884 18513608884 18612625555 13141108580 13161146667 13263389994 13264459992 18600004038 18612932226',相匹配出后四位至少有一个是8的手机号,正则该怎么写?
不要穷举。如(8\d{3}|\d8\d\d|\d\d8\d|\d{3}8\d)。
零宽断言能写吗

php代码:

<code>$str = '13141078884 13161143334 13263362224 18511925554 18612623450 13141102288 13161146662 13263370884 18513608884 18612625555 13141108580 13161146667 13263389994 13264459992 18600004038 18612932226';
// 匹配手机号后四位至少有一个是8
preg_match_all('/????????/', $str, $res);
var_dump($res);
</code>

回复内容:

我有一串手机号:'13141078884 13161143334 13263362224 18511925554 18612623450 13141102288 13161146662 13263370884 18513608884 18612625555 13141108580 13161146667 13263389994 13264459992 18600004038 18612932226',相匹配出后四位至少有一个是8的手机号,正则该怎么写?
不要穷举。如(8\d{3}|\d8\d\d|\d\d8\d|\d{3}8\d)。
零宽断言能写吗

php代码:

<code>$str = '13141078884 13161143334 13263362224 18511925554 18612623450 13141102288 13161146662 13263370884 18513608884 18612625555 13141108580 13161146667 13263389994 13264459992 18600004038 18612932226';
// 匹配手机号后四位至少有一个是8
preg_match_all('/????????/', $str, $res);
var_dump($res);
</code>

<code>(?=(.?){3}8)(\d{4})
(?=\d*8)(\d{4})
(?![^8]{4})(\d{4})  // 受@chouchang折叠答案的启发
</code>

PHP测试可以匹配

<code>$str = '13141078884 13161143334 13263362224 18511925554 18612623450 13141102288 13161146662 13263370884 18513608884 18612625555 13141108580 13161146667 13263389994 13264459992 18600004038 18612932226';
// 匹配手机号后四位至少有一个是8
preg_match_all('/\d{7}(?=(.?){3}8)(\d{4})/', $str, $res);
var_dump($res);
</code>

<code>/8(?:\d{0,3})$/
</code>

保证11位数字:

<code>/^(?=\d{11})(?=\d*8\d{0,3}).*$/

//还是没有办法,期待其他人能想出来
</code>

是我想多了,应该这么写:

<code>/^(?=\d*8\d{0,3})\d{11}$/
</code>

正则匹配手机号后四位至少有一个是8?如何写?

/[^8]{4}$/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn