search
HomeBackend DevelopmentPHP Tutorialphp实战正则表达式(一):验证手机号

本文通过逐步完善一个验证手机号的正则表达式来介绍了正则表达式中的字符组、量词、字符串起始/结束位置、分组、分组中的选择结构、反向引用、命名分组等概念。

1 基本验证

即验证字符串是否是11位数字。

表达式

  • [0123456789]{11}

  • 或[0-9]{11}

  • 或 \d{11}

知识点

字符组:正则表达式中用方括号对[...]表示字符组。字符组表示在同一位置可能出现的字符。
如,[0123456789]表示匹配数字0123456789中的任意一个;[0123abc]匹配数字0123和字母abc中的任意一个。

字符组的范围表示法:字符组中使用短横线([..-..])来表示一段范围的字符。
如,[a-z]表示匹配所有小写英文字母中的任意一个;[a-zA-Z]表示匹配所有小写英文字母和大写英文字母中的一个;[0-9]表示匹配0123456789中的任意一个。
要注意的是,默认范围是起始字符的ACSⅡ码到结束字符的ACSⅡ码之间的字符

字符组简记法:对于一些常用的字符组,正则表达式规定了一些简记符号来表示它们。

  • \d 所有的数字,即[0-9]

  • \D 所有的非数字,与\d互斥

  • \w 所有的单词字符(字符、数字、下划线),即[0-9a-zA-Z_]

  • \W 所有的非单词字符,与\W互斥

  • \s 所有的空白字符,包括空格、制表符、回车符、换行符等空白字符

  • \S 所有的非空白字符,与\s互斥

量词:量词表示它所修饰的对象(如字符、字符组)可能出现的次数。
量词的一般形式是{m,n}(逗号,后面不能有空格),表示它所修饰的字符(或字符组)的出现次数大于等于m次,小于等于n次。特别地

  • {m}表示修饰对象只能出现m次;

  • {0,n}表示修饰的对象最多出现n次,最少出现0次;

  • {m,}表示修饰的对象最少出现m次。

2 长度真的只能为11?

观察下面的gif中的代码可以看到,当输入的字符串是长度为15的数字时,也可以匹配前面11个数字。甚至输入字符是abcd180123412341234时也可以匹配到11个数字。

这是因为上面的正则表达式的含义是“匹配11个数字”,因此只要输入的字符串中有连续的11个数字就可以匹配成功。要想验证输入的字符串仅仅是手机号,需要使用正则表达式中的字符串起始位置^和字符串结束位置$。

表达式

  • ^\d{11}$

知识点

正则表达式中有一些符号匹配的是位置,而不是文本,这类符号叫做锚点(anchor)。^、$就是其中两个。

^ 匹配的位置是字符串的开始位置
$ 匹配的位置是字符串的结束位置

3 更严谨的验证

我们都知道国内常见的手机号都是以130-139,150-153、155-159、180、182、185-189开头,此外,还有170、176-178等。我们上一节得到的表达式对手机开头并没有进行验证。

表达式

^1(3[0-9]|5[012356789]|8[0256789]|7[0678])\d{8}$

知识点

分组:正则表达式中可以用圆括号对(...)表示一个分组(子表达式),这样在匹配的结果中除了会返回全部匹配到的内容,还会返回每个子表达式各自匹配到的内容。如上图中的表达式执行后的结果,数组的第0个元素为整个正则表达式匹配到的值,第1个元素为圆括号对内的正则匹配到的值。

preg_match('/^1(3[0-9]|5[012356789]|8[0256789]|7[0678])\d{8}$/', '18012341234', $arr);print_r($arr);/*Array(    [0] => 18012341234    [1] => 80)*/

选择结构:圆括号对(...)内的子表达式用竖线|隔开表示不同的选择,圆括号内的整个正则可以匹配任意一个选择。
例如,(3[0-9]|5[012356789]|8[0256789]|7[0678])表示这里匹配的值可以是3[0-9]或者5[012356789]或者8[0256789]或者7[0678]中的任意一个。

4 锦上添花

有些时候,手机号中间会有-符号,变成180-1234-1234的形式,比如现在的iPhone会自动将手机号转为这种格式。

依据到目前为止介绍的一些知识,可以写出下面的正则表达式来兼容180-1234-1234的形式:

^1(3[0-9]|5[012356789]|8[0256789]|7[0678])-{0,1}\d{4}-{0,1}\d{4}$

其中-{0,1}表示字符-可以出现1次或者不出现,这是我们前面了解过的量词,其实在正则表达式中,对这种常用量词还规定了特殊的记法:

  • ? 相当于{0,1},可以出现0次或1次

  • + 相当于{1,},出现次数大于等于1次

  • * 相当于{0,},出现次数大于等于0次

因此,上面的正则表达式也等价于

^1(3[0-9]|5[012356789]|8[0256789]|7[0678])-?\d{4}-?\d{4}$

但是,上面的表达式除了能匹配18012341234和180-1234-1234,其实也能匹配180-12341234、1801234-1234这两种形式。
如果我们只想匹配18012341234和180-1234-1234这两种形式,可以使用正则表达式中的反向引用:

^1(3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\2\d{4}$

上面的\2就是反向引用,它是匹配第二个圆括号对(...)匹配到的内容。反向引用的形式是\num,它是在正则表达式里面引用前面的分组匹配到的内容。

上面的正则表达式中,我们用\2来进行反向引用,然而\1却没有什么用,那么我们可不可以忽略那些不用的分组呢?正则表达式中的非捕获分组可以满足这个需求:

^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\1\d{4}$

上面的(?:3[0-9]|5[012356789]|8[0256789]|7[0678])就是非捕获分组。非捕获的形式是(?:...),使用了非捕获分组后,匹配的结果里面不再有该分组匹配到的结果。

上面对分组的引用是基于子表达式的编号,当正则表达式比较复杂或编号太多时要弄清楚每个分组的编号是一件很痛苦的事情。因此,正则表达式提供了命名分组

^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(?P-?)\d{4}(?P=separato)\d{4}$

上面正则表达式中的(?P-?)就是命名分组。命名分组的形式是(?P...),命名分组的引用使用(?P=name)的形式。

5 总结

到此为止,一个健壮的验证手机号的正则表达式就完成了。虽然功能很简单,但是还是涉及到了正则表达式中不少的知识点。

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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.