教程看了千千万,最后不动手都是白搭,结合实例看一下,多多实践才能真正学会. 最简单的匹配 ^\d{5,}$#QQ号码为数字且至少5位 ^ :表示匹配字符串的开始,也即该字符串是独立的开始而不是包含在某个字符串之内 \d :表示匹配数字 {5,} :表示至少匹配5位及以上
教程看了千千万,最后不动手都是白搭,结合实例看一下,多多实践才能真正学会.
最简单的匹配
^\d{5,}$ #QQ号码为数字且至少5位
-
^
:表示匹配字符串的开始,也即该字符串是独立的开始而不是包含在某个字符串之内 -
\d
:表示匹配数字 -
{5,}
:表示至少匹配5位及以上 -
$
:表示匹配字符串的结束,也即该字符串是独立的结束
从该例子可以看出,正则表达式是从左至右描述的.
在来个类似的例子:
^1\d{10}$ #1开头的11位(独立)数字,可以简单认为是手机号码
元字符
在上面的例子中,^
、\d
及 $
等这些符号,代表了特定的匹配意义,我们称之为元字符,常用的元字符如下:
元字符 | 说明 |
---|---|
. |
匹配除换行符意外的任意字符 |
\w |
匹配字母或数字或下划线 |
\s |
匹配任意的空白符 |
\d |
匹配数字 |
\b |
匹配单词的开始或结束 |
^ |
匹配字符串的开始 |
$ |
匹配字符串的结束 |
[x] |
匹配x字符,如匹配字符串中的 a、b 和 c 字符 |
\W |
\w的反义,即匹配任意非字母,数字,下划线和汉字的字符 |
\S |
\s的反义,即匹配任意非空白符的字符 |
\D |
\d的反义,即匹配任意非数字的字符 |
\B |
\b的反义,即不是单词开头或结束的位置 |
[^x] |
匹配除了 x 意外的任意字符,如 [^abc] 匹配除了 abc 这几个字母之外的任意字符 |
提示
- 当我们要匹配这些元字符的时候,我们需要用到字符转义功能,同样正则表达式里面用 \ 来表示转义,如要匹配 . 符号,则需要用
\.
,否则.
会被解释成“除换行符外的任意字符”。当然,要匹配\
,则需要写成\\
- 连续的数字或字母可以用
–
符号连接起来,如 匹配所有的小写字母,[1-5]
匹配 1 至 5 这 5 个数字
原文地址:PHP中的正则表达式应用, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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.

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 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.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor