正则

WBOY
Release: 2016-06-23 14:20:43
Original
1017 people have browsed it

$pi = "/(\w{4})(\d{3})-(\d{4})(\w{2})(\d{1}?)-(2)/x";$phones ="Call555-1212or1-800-555-1212";echo preg_match($pi,$phones)? "1": "2";
Copy after login

这样他就不行
$pi = "/(\w{4})(\d{3})-(\d{4})(\w{2})(\d{1}?)-/x";$phones ="Call555-1212or1-800-555-1212";echo preg_match($pi,$phones)? "1": "2";
Copy after login

这样就行!神马回事啊


回复讨论(解决方案)

$pi = "/(\w{4})(\d{3})-(\d{4})(\w{2})(\d{1}?)-/x";
匹配到的是
Array
(
    [0] => Call555-1212or1-
    [1] => Call
    [2] => 555
    [3] => 1212
    [4] => or
    [5] => 1
)

如果加上 (2)
则需要 $phones ="Call555-1212or1- 800-555-1212"; 中套红的位置为 2 才能匹配成功

$pi = "/(\w{4})(\d{3})-(\d{4})(\w{2})(\d{1}?)-/x";
匹配到的是
Array
(
    [0] => Call555-1212or1-
    [1] => Call
    [2] => 555
    [3] => 1212
    [4] => or
    [5] => 1
)

如果加上 (2)
则需要 $phones ="Call555-1212or1- 800-555-1212"; 中套红的位置为 2 才能匹配成功
有点不理解你的意思!

$pi = "/(\w{4})(\d{3})-(\d{4})(\w{2})(\d{1}?)-/x";
匹配到的是
Array
(
    [0] => Call555-1212or1-
    [1] => Call
    [2] => 555
    [3] => 1212
    [4] => or
    [5] => 1
)

如果加上 (2)
则需要 $phones ="Call555-1212or1- 800-555-1212"; 中套红的位置为 2 才能匹配成功
(2)不就是(\d{3})!而且我试过给他命一个名然后调用也不对!只能是位置的问题!但是我找不出原因啊!

(2) 表示匹配字符 2,并可向后引用
如果你想引用 (\d{3}) 则需要写作 \\2

(2) 表示匹配字符 2,并可向后引用
如果你想引用 (\d{3}) 则需要写作 \\2
$pi = "/(\w{4})(?\d{3})-(\d{4})(\w{2})(\d{1}?)-(\k)/x";
请问这样写对吗?

(2) 表示匹配字符 2,并可向后引用
如果你想引用 (\d{3}) 则需要写作 \\2
好像还是匹配不了、

不管其他写什么 -2 在你的字串里就肯定没有,-(2)就必然匹配不到

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template