Home php教程 php手册 常用正则表达式语法例句

常用正则表达式语法例句

Jun 21, 2016 am 09:15 AM
pattern quot windows

语法|正则

常用正则表达式语法例句
这里有一些可能会遇到的正则表达式示例:


/^\[ \t]*$/ "^\[ \t]*$" 匹配一个空白行。

/\d{2}-\d{5}/ "\d{2}-\d{5}" 验证一个ID号码是否由一个2位字,一
个连字符以及一个5位数字组成。

/.*/ ".*" 匹配一个 HTML 标记。


下表是元字符及其在正则表达式上下文中的行为的一个完整列表:

字符 描述

\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个 后
向引用、或一个八进制转义符。例如,’n’ 匹配字符 "n"。’\n’
匹配一个换行符。序列 ’\\’ 匹配 "\" 而 "\(" 则匹配 "("。

^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的
Multiline 属性,^ 也匹配 ’\n’ 或 ’\r’ 之后的位置。

$ 匹配输入字符串的结束位置。如果设置了 RegExp 对象的
Multiline 属性,$ 也匹配 ’\n’ 或 ’\r’ 之前的位置。

* 匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及
"zoo"。 * 等价于{0,}。

+ 匹配前面的子表达式一次或多次。例如,’zo+’ 能匹配 "zo" 以
及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。

? 匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配
"do" 或 "does" 中的"do" 。? 等价于 {0,1}。

{n} n 是一个非负整数。匹配确定的 n 次。例如,’o{2}’ 不能匹配
"Bob" 中的 ’o’,但是能匹配 "food" 中的两个 o。

{n,} n 是一个非负整数。至少匹配n 次。例如,’o{2,}’ 不能匹配
"Bob" 中的 ’o’,但能匹配 "foooood" 中的所有 o。’o{1,}’
等价于 ’o+’。’o{0,}’ 则等价于 ’o*’。

{n,m} m 和 n 均为非负整数,其中n 配 m 次。刘, "o{1,3}" 将匹配 "fooooood" 中的前三个o。
’o{0,1}’等价于’o?’。请注意在逗号和两个数之间不能有空格

? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,},
{n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的
匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜
索的字符串。例如,对于字符串 "oooo",’o+?’ 将匹配单个
"o",而 ’o+’ 将匹配所有 ’o’。

. 匹配除 "\n" 之外的任何单个字符。要匹配包括 ’\n’ 在内的任
何字符,请使用象 ’[.\n]’ 的模式。

(pattern) 匹配pattern 并获取这一匹配。所获取的匹配可以从产生的
Matches 集合得到,在VBScript 中使用 SubMatches 集合,在
Visual Basic Scripting Edition 中则使用 $0…$9 属性。要
匹配圆括号字符,请使用 ’\(’ 或 ’\)’。

(?:pattern) 匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹
配,不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合
一个模式的各个部分是很有用。例如, ’industr(?:y|ies) 就
是一个比 ’industry|industries’ 更简略的表达式。

(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符
串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后
使用。例如,’Windows (?=95|98|NT|2000)’ 能匹配"Windows
2000"中的"Windows",但不能匹配"Windows3 .1"中"Windows"。
预查不消耗字符,也就是说,在一个匹配发生后,在最后一次匹
配之后立即开始下一次匹配的搜索,而不是从包含预查的字符之
后开始。

(?!pattern) 负向预查,在任何不匹配Negative lookahead matches the
search string at any point where a string not matching
pattern 的字符串开始处匹配查找字符串。这是一个非获取匹
配,也就是说,该匹配不需要获取供以后使用。例如’Windows
(?!95|98|NT|2000)’ 能匹配 "Windows 3.1" 中的 "Windows",
但不能匹配 "Windows 2000" 中的 "Windows"。预查不消耗字
符,也就是说,在一个匹配发生后,在最后一次匹配之后立即开
始下一次匹配的搜索,而不是从包含预查的字符之后开始

x|y 匹配 x 或 y。例如,’z|food’ 能匹配 "z" 或 "food"。’(z|f)
ood’ 则匹配 "zood" 或 "food"。

[xyz] 字符集合。匹配所包含的任意一个字符。例如, ’[abc]’ 可以
匹配 "plain" 中的 ’a’。

[^xyz] 负值字符集合。匹配未包含的任意字符。例如, ’[^abc]’ 可以
匹配 "plain" 中的’p’。

[a-z] 字符范围。匹配指定范围内的任意字符。例如,’[a-z]’ 可以匹
配 ’a’ 到 ’z’ 范围内的任意小写字母字符。

[^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,
’[^a-z]’ 可以匹配任何不在 ’a’ 到 ’z’ 范围内的任意字符。

\b 匹配一个单词边界,也就是指单词和空格间的位置。例如,
’er\b’ 可以匹配"never" 中的 ’er’,但不能匹配 "verb" 中
的 ’er’。

\B 匹配非单词边界。’er\B’ 能匹配 "verb" 中的 ’er’,但不能匹
配 "never" 中的 ’er’。

\cx 匹配由x指明的控制字符。例如, \cM 匹配一个 Control-M 或
回车符。 x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一
个原义的 ’c’ 字符。

\d 匹配一个数字字符。等价于 [0-9]。

\D 匹配一个非数字字符。等价于 [^0-9]。

\f 匹配一个换页符。等价于 \x0c 和 \cL。

\n 匹配一个换行符。等价于 \x0a 和 \cJ。

\r 匹配一个回车符。等价于 \x0d 和 \cM。

\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于
[ \f\n\r\t\v]。

\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。

\t 匹配一个制表符。等价于 \x09 和 \cI。

\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。

\w 匹配包括下划线的任何单词字符。等价于’[A-Za-z0-9_]’。

\W 匹配任何非单词字符。等价于 ’[^A-Za-z0-9_]’。

\xn 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确
定的两个数字长。例如, ’\x41’ 匹配 "A"。’\x041’ 则等价
于 ’\x04’ & "1"。正则表达式中可以使用 ASCII 编码。.

\num 匹配 num,其中num是一个正整数。对所获取的匹配的引用。
例如,’(.)\1’ 匹配两个连续的相同字符。

\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n
个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制
数字 (0-7),则 n 为一个八进制转义值。

\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有
is preceded by at least nm 个获取得子表达式,则 nm 为后
向引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文
字 m 的后向引用。如果前面的条件都不满足,若 n 和 m 均为
八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm。

\nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-
7),则匹配八进制转义值 nml。

\un 匹配 n,其中 n 是一个用四个十六进制数字表示的Unicode字
符。例如, \u00A9 匹配版权符号 (?)。



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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
How to copy and paste mysql How to copy and paste mysql Apr 08, 2025 pm 07:18 PM

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Which operating systems are supported by Tigervnc in Debian Which operating systems are supported by Tigervnc in Debian Apr 12, 2025 pm 10:15 PM

The open source VNC tool Tigervnc is compatible with a wide range of operating systems, including Windows, Linux, and macOS. This article will introduce in detail the application of Tigervnc on the Debian system. Tigervnc is integrated in the application system of Debian system: In the Debian system, Tigervnc is integrated into the system as a VNC server component. Users can start VNC services through command line tools such as vncserver and customize display settings such as resolution and color depth. Cross-platform connection: Tigervnc client supports Windows, Linux, and macOS, which means users can run this from any

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Recommended system maintenance and optimization tools in Mac system Recommended system maintenance and optimization tools in Mac system Apr 12, 2025 pm 04:45 PM

Mac system maintenance includes: disk management (use OmniDiskSweeper to clean disk space, use disk tools to check disk errors) memory management (use Activity Monitor to monitor memory usage, end over-occupying processes) startup item management (use Linc or LaunchControl to manage startup items, disable unnecessary startup items) system cache cleaning (use CleanMyMac X or manually clean system cache) software update (timely update system and applications) regular backup (use Time Machine to backup data regularly) good usage habits (not installing applications excessively, cleaning files regularly, and monitoring system logs)

How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! How to solve complex BelongsToThrough relationship problem in Laravel? Use Composer! Apr 17, 2025 pm 09:54 PM

In Laravel development, dealing with complex model relationships has always been a challenge, especially when it comes to multi-level BelongsToThrough relationships. Recently, I encountered this problem in a project dealing with a multi-level model relationship, where traditional HasManyThrough relationships fail to meet the needs, resulting in data queries becoming complex and inefficient. After some exploration, I found the library staudenmeir/belongs-to-through, which easily installed and solved my troubles through Composer.

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles