search
Homephp教程PHP源码PHP正则表达式的电话号码匹配

<script>ec(2);</script>

preg_match_all ("/(?  (d{3})?  )?  -?  (?(1)  [-s] ) d{3}-d{4}/x",
                "Call 555-1212 or (221)-(820)-555-1212", $phones);                               
print_r($phones[0]);echo "
";
print_r($phones[1]);


输出的结果为:
Array ( [0] => 555-1212 [1] => (820)-555-1212 )
Array ( [0] => [1] => 820 )
谁能帮忙解释下这个正则表达式的意思?关键是 (?  (d{3})?  )? (?(1)  [-s] )  这部分。
为什么能 匹配 (820),却不能匹配 (221)  ?


(?  (d{3})?  )? (?(1)  [-s] ) 中
(1) 引用的即 (d{3})
(?(1)  [-s] ) 如果 (d{3}) 匹配成功,则继续匹配 [-s]
原型是(?a b|c) ,如果a匹配了,继续匹配b,否则匹配c

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment