正则表达式教程目录正则表达式教程目录正则表达式教程目录正则表达式教程目录
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
正则表达式教程目录
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式教程 » 正则表达式教程目录

Regex 教学
简介
目录
特殊字符
不可打印字符
Regex 引擎内部
字符类别
字符类别减法
字符类别交集
简写字符类别
点
锚点
字词边界
交替
可选项目
重复
群组和截取
反向引用
反向引用,第 2 部分
命名组
相对反向引用
分支重设群组
自由间距和注解
Unicode
模式修改器
原子组
独占量词
前瞻和后顾
环顾,第 2 部分
将文本保留在比对之外
条件式
平衡组
递归
子常式
无限递归
递归和量词
递归和截取
递归和反向引用
递归和回溯
POSIX 方括号表达式
零长度比对
继续比对
本网站的其他内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

正则表达式教程目录

本正则表达式教程会教导您正则表达式的每个面向。每个主题都假设您已阅读并了解所有先前的主题。如果您是正则表达式的新手,您应该按照呈现的顺序阅读这些主题。

简介

简介说明教学的范围和讨论哪些 regex 风格。它也会介绍基本术语。

文本字符和特殊字符

最简单的 regex 仅包含文本字符。某些字符在 regex 中有特殊意义,必须加上转义字符。在软件原代码中使用 regex 时,转义规则可能会变得有点复杂。

不可打印字符

使用控制字符转义或十六进位转义,可以更轻松地输入不可打印字符,例如控制字符和特殊间距或换行字符。

Regex 引擎如何内部运作

首先查看正则表达式引擎内部的内部结构。后面的主题创建在这些信息上。了解引擎的内部结构有助于您创建符合预期,且不符合您不想要的正则表达式。

字符类别或字符集

字符类别或字符集会比对出几个可能的字符中的单一字符,包括个别字符和/或字符范围。否定字符类别会比对出字符类别中不存在的单一字符。

简写字符类别

简写字符类别让您可以快速使用常见的字符集。您可以单独使用简写,或将其用于字符类别的一部分。

字符类别减法

字符类别减法让您可以比对出存在于一个字符集,但不存在于另一个字符集中的字符。

字符类别交集

字符类别交集让您可以比对出存在于一个字符集,且也存在于另一个字符集中的字符。

点

点会比对任何字符,但通常不会比对换行字符,除非您变更选项。

锚点

锚点长度为零。它们不会比对任何字符,而是比对位置。有锚点可以比对主旨字符串的开头和结尾,以及比对每一行的开头和结尾。

字词边界

字词边界就像锚点,但会比对字词的开头和/或结尾。

交替

通过用直线分隔不同的子正则表达式,您可以指示正则表达式引擎从左到右尝试这些子正则表达式,并在其中一个可以比对时立即传回成功。

可选项目

在项目后加上问号,会指示正则表达式引擎在可能的情况下比对该项目,但如果无法比对,则继续进行(而不是承认失败)。

使用各种量词进行重复

星号、加号和花括号这三种样式的操作符让您可以重复一个项目零次或多次、一次或多次,或任意次数。重要的是,除非您明确地将这些量词设为「惰性」,否则它们缺省为「贪婪」。

群组

通过在正则表达式的一部分周围加上括号,您可以指示引擎在套用量词或将替代方案分组在一起时,将该部分视为单一项目。括号也会创建捕获组,让您可以重复使用正则表达式的一部分所比对到的文本。

反向引用

对捕获组的反向引用会比对先前由该捕获组比对到的相同文本,让您可以比对重复文本的模式。

命名组和反向引用

如果使用命名的捕获组和命名的反向引用,则具有多个群组的正则表达式更容易阅读和维护。

分支重设群组

在使用交替来比对同一件事的不同变体时,可以将变体放入分支重设群组中。然后,所有变体共用相同的捕获组。这允许您使用反向引用或截取比对文本的一部分,而无需检查哪个变体截取了它。

自由间距和注解

将正则表达式拆分为多行,添加注解和空白,使其更容易阅读和理解。

Unicode 字符和属性

如果您的正则表达式风格支持 Unicode,则可以使用特殊的 Unicode 正则表达式代码来比对特定的 Unicode 字符,或比对具有特定 Unicode 属性或属于特定 Unicode 脚本或区块的任何字符。

模式修改器

变更比对模式,例如正则表达式特定部分的「不区分大小写」。

原子组和占有量词

嵌套量词会导致指数级增加的回溯,使正则表达式引擎陷入停顿。原子组和占有量词提供了解决方案。

具备零长度断言的环顾,第 1 部分 和 第 2 部分

通过环顾(统称为前瞻和后顾),您可以找到后接或不后接特定文本,以及前接或不前接特定文本的比对,而无需将前接或后接文本包含在整体正则表达式比对中。您也可以使用环顾来针对多个需求测试比对的相同部分。

将迄今为止比对的文本保持在整体正则表达式比对之外

将迄今为止比对的文本保持在整体正则表达式比对之外,允许您找到前接特定文本的比对,而无需将该前接文本包含在整体正则表达式比对中。此方法主要适用于对后顾支持有限或不支持后顾的正则表达式风格。

条件式

条件式是一种特殊结构,它会先评估环顾或反向引用,然后在环顾成功时运行一个子正则表达式,在环顾失败时运行另一个子正则表达式。

递归

递归在正则表达式中特定点再次比对整个正则表达式,这使得比对平衡结构成为可能。

子常式调用

子常式调用让您可以撰写正则表达式,在多个地方比对相同的结构,而无需重复正则表达式的部分。

递归、子常式和截取

支持递归和子常式调用的正则表达式类型,会以不同的方式处理递归和子常式调用中的捕获组。

具有递归层级的反向引用

特殊反向引用会比对由捕获组保存在特定递归层级的文本,而不是该捕获组最近比对到的文本。

递归、子常式和回溯

支持递归和子常式调用的正则表达式类型,在递归或子常式调用失败后,会以不同的方式进行回溯。

POSIX 方括号表达式

如果您使用 兼容 POSIX 的正则表达式引擎,您可以使用 POSIX 方括号表达式比对与当地相关的字符。

零长度比对问题

当正则表达式可以找到零长度比对时,正则表达式引擎会使用不同的策略,避免在您想要反复运算字符串中的所有比对时,卡在零长度比对上。这可能会导致不同的比对结果。

从前一次比对尝试继续

强制正则表达式比对从前一次比对的结尾开始,提供一种解析文本数据的有效率方式。

正規表示式教學目錄
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學 » 正規表示式教學目錄

Regex 教學
簡介
目錄
特殊字元
不可列印字元
Regex 引擎內部
字元類別
字元類別減法
字元類別交集
簡寫字元類別
點
錨點
字詞邊界
交替
可選項目
重複
群組和擷取
反向參照
反向參照,第 2 部分
命名群組
相對反向參照
分支重設群組
自由間距和註解
Unicode
模式修改器
原子群組
獨佔量詞
前瞻和後顧
環顧,第 2 部分
將文字保留在比對之外
條件式
平衡群組
遞迴
子常式
無限遞迴
遞迴和量詞
遞迴和擷取
遞迴和反向參照
遞迴和回溯
POSIX 方括號表示式
零長度比對
繼續比對
本網站的其他內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

正規表示式教學目錄

本正規表示式教學會教導您正規表示式的每個面向。每個主題都假設您已閱讀並了解所有先前的主題。如果您是正規表示式的新手,您應該按照呈現的順序閱讀這些主題。

簡介

簡介說明教學的範圍和討論哪些 regex 風格。它也會介紹基本術語。

文字字元和特殊字元

最簡單的 regex 僅包含文字字元。某些字元在 regex 中有特殊意義,必須加上跳脫字元。在軟體原始碼中使用 regex 時,跳脫規則可能會變得有點複雜。

不可列印字元

使用控制字元跳脫或十六進位跳脫,可以更輕鬆地輸入不可列印字元,例如控制字元和特殊間距或換行字元。

Regex 引擎如何內部運作

首先檢視正規表示式引擎內部的內部結構。後面的主題建立在這些資訊上。瞭解引擎的內部結構有助於您建立符合預期,且不符合您不想要的正規表示式。

字元類別或字元集

字元類別或字元集會比對出幾個可能的字元中的單一字元,包括個別字元和/或字元範圍。否定字元類別會比對出字元類別中不存在的單一字元。

簡寫字元類別

簡寫字元類別讓您可以快速使用常見的字元集。您可以單獨使用簡寫,或將其用於字元類別的一部分。

字元類別減法

字元類別減法讓您可以比對出存在於一個字元集,但不存在於另一個字元集中的字元。

字元類別交集

字元類別交集讓您可以比對出存在於一個字元集,且也存在於另一個字元集中的字元。

點

點會比對任何字元,但通常不會比對換行字元,除非您變更選項。

錨點

錨點長度為零。它們不會比對任何字元,而是比對位置。有錨點可以比對主旨字串的開頭和結尾,以及比對每一行的開頭和結尾。

字詞邊界

字詞邊界就像錨點,但會比對字詞的開頭和/或結尾。

交替

透過用直線分隔不同的子正規表示式,您可以指示正規表示式引擎從左到右嘗試這些子正規表示式,並在其中一個可以比對時立即傳回成功。

可選項目

在項目後加上問號,會指示正規表示式引擎在可能的情況下比對該項目,但如果無法比對,則繼續進行(而不是承認失敗)。

使用各種量詞進行重複

星號、加號和花括號這三種樣式的運算子讓您可以重複一個項目零次或多次、一次或多次,或任意次數。重要的是,除非您明確地將這些量詞設為「惰性」,否則它們預設為「貪婪」。

群組

透過在正規表示式的一部分周圍加上括號,您可以指示引擎在套用量詞或將替代方案分組在一起時,將該部分視為單一項目。括號也會建立擷取群組,讓您可以重複使用正規表示式的一部分所比對到的文字。

反向參照

對擷取群組的反向參照會比對先前由該擷取群組比對到的相同文字,讓您可以比對重複文字的模式。

命名群組和反向參照

如果使用命名的擷取群組和命名的反向參照,則具有多個群組的正規表示式更容易閱讀和維護。

分支重設群組

在使用交替來比對同一件事的不同變體時,可以將變體放入分支重設群組中。然後,所有變體共用相同的擷取群組。這允許您使用反向參照或擷取比對文字的一部分,而無需檢查哪個變體擷取了它。

自由間距和註解

將正規表示式拆分為多行,新增註解和空白,使其更容易閱讀和理解。

Unicode 字元和屬性

如果您的正規表示式風格支援 Unicode,則可以使用特殊的 Unicode 正規表示式代碼來比對特定的 Unicode 字元,或比對具有特定 Unicode 屬性或屬於特定 Unicode 腳本或區塊的任何字元。

模式修改器

變更比對模式,例如正規表示式特定部分的「不區分大小寫」。

原子群組和佔有量詞

巢狀量詞會導致指數級增加的回溯,使正規表示式引擎陷入停頓。原子群組和佔有量詞提供了解決方案。

具備零長度斷言的環顧,第 1 部分 和 第 2 部分

透過環顧(統稱為前瞻和後顧),您可以找到後接或不後接特定文字,以及前接或不前接特定文字的比對,而無需將前接或後接文字包含在整體正規表示式比對中。您也可以使用環顧來針對多個需求測試比對的相同部分。

將迄今為止比對的文字保持在整體正規表示式比對之外

將迄今為止比對的文字保持在整體正規表示式比對之外,允許您找到前接特定文字的比對,而無需將該前接文字包含在整體正規表示式比對中。此方法主要適用於對後顧支援有限或不支援後顧的正規表示式風格。

條件式

條件式是一種特殊結構,它會先評估環顧或反向參照,然後在環顧成功時執行一個子正規表示式,在環顧失敗時執行另一個子正規表示式。

遞迴

遞迴在正規表示式中特定點再次比對整個正規表示式,這使得比對平衡結構成為可能。

子常式呼叫

子常式呼叫讓您可以撰寫正規表示式,在多個地方比對相同的結構,而無需重複正規表示式的部分。

遞迴、子常式和擷取

支援遞迴和子常式呼叫的正規表示式類型,會以不同的方式處理遞迴和子常式呼叫中的擷取群組。

具有遞迴層級的反向參照

特殊反向參照會比對由擷取群組儲存在特定遞迴層級的文字,而不是該擷取群組最近比對到的文字。

遞迴、子常式和回溯

支援遞迴和子常式呼叫的正規表示式類型,在遞迴或子常式呼叫失敗後,會以不同的方式進行回溯。

POSIX 方括號表示式

如果您使用 相容 POSIX 的正規表示式引擎,您可以使用 POSIX 方括號表示式比對與當地相關的字元。

零長度比對問題

當正規表示式可以找到零長度比對時,正規表示式引擎會使用不同的策略,避免在您想要反覆運算字串中的所有比對時,卡在零長度比對上。這可能會導致不同的比對結果。

從前一次比對嘗試繼續

強制正規表示式比對從前一次比對的結尾開始,提供一種解析文字資料的有效率方式。

Regular Expressions Tutorial Table of Contents
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial » Regular Expressions Tutorial Table of Contents

Regex Tutorial
Introduction
Table of Contents
Special Characters
Non-Printable Characters
Regex Engine Internals
Character Classes
Character Class Subtraction
Character Class Intersection
Shorthand Character Classes
Dot
Anchors
Word Boundaries
Alternation
Optional Items
Repetition
Grouping & Capturing
Backreferences
Backreferences, part 2
Named Groups
Relative Backreferences
Branch Reset Groups
Free-Spacing & Comments
Unicode
Mode Modifiers
Atomic Grouping
Possessive Quantifiers
Lookahead & Lookbehind
Lookaround, part 2
Keep Text out of The Match
Conditionals
Balancing Groups
Recursion
Subroutines
Infinite Recursion
Recursion & Quantifiers
Recursion & Capturing
Recursion & Backreferences
Recursion & Backtracking
POSIX Bracket Expressions
Zero-Length Matches
Continuing Matches
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Regular Expressions Tutorial Table of Contents

This regular expressions tutorial teaches you every aspect of regular expressions. Each topic assumes you have read and understood all previous topics. If you are new to regular expressions, you should read the topics in the order presented.

Introduction

The introduction indicates the scope of the tutorial and which regex flavors are discussed. It also introduces basic terminology.

Literal Characters and Special Characters

The simplest regex consists of only literal characters. Certain characters have special meanings in a regex and have to be escaped. Escaping rules may get a bit complicated when using regexes in software source code.

Non-Printable Characters

Non-printable characters such as control characters and special spacing or line break characters are easier to enter using control character escapes or hexadecimal escapes.

How a Regex Engine Works Internally

First look at the internals of the regular expression engine’s internals. Later topics build on this information. Knowing the engine’s internals greatly helps you to craft regexes that match what you intended, and not match what you do not want.

Character Classes or Character Sets

A character class or character set matches a single character out of several possible characters, consisting of individual characters and/or ranges of characters. A negated character class matches a single character not in the character class.

Shorthand Character Classes

Shorthand character classes allow you to use common sets of characters quickly. You can use shorthands on their own or as part of character classes.

Character Class Subtraction

Character class subtraction allows you to match one character that is present in one set of characters but not present in another set of characters.

Character Class Intersection

Character class intersection allows you to match one character that is present in one set of characters and also present in another set of characters.

The Dot

The dot matches any character, though usually not line break characters unless you change an option.

Anchors

Anchors are zero-length. They do not match any characters, but rather a position. There are anchors to match at the start and end of the subject string, and anchors to match at the start and end of each line.

Word Boundaries

Word boundaries are like anchors, but match at the start of a word and/or the end of a word.

Alternation

By separating different sub-regexes with vertical bars, you can tell the regex engine to attempt them from left to right, and return success as soon as one of them can be matched.

Optional Items

Putting a question mark after an item tells the regex engine to match the item if possible, but continue anyway (rather than admit defeat) if it cannot be matched.

Repetition Using Various Quantifiers

Three styles of operators, the star, the plus, and curly braces, allow you to repeat an item zero or more times, once or more, or an arbitrary number of times. It is important to understand that these quantifiers are “greedy” by default, unless you explicitly make them “lazy”.

Grouping

By placing parentheses around part of the regex, you tell the engine to treat that part as a single item when applying quantifiers or to group alternatives together. Parentheses also create capturing groups allow you to reuse the text matched by part of the regex.

Backreferences

Backreferences to capturing groups match the same text that was previously matched by that capturing group, allowing you to match patterns of repeated text.

Named Groups and Backreferences

Regular expressions that have multiple groups are much easier to read and maintain if you use named capturing groups and named backreferences.

Branch Reset Groups

When using alternation to match different variants of the same thing, you can put the alternatives in a branch reset group. Then all the alternatives share the same capturing groups. This allows you to use backreferences or retrieve part of the matched text without having to check which of the alternatives captured it.

Free-Spacing and Comments

Splitting a regular expression into multiple lines, adding comments and whitespace, makes it easier to read and understand.

Unicode Characters and Properties

If your regular expression flavor supports Unicode, then you can use special Unicode regex tokens to match specific Unicode characters, or to match any character that has a certain Unicode property or is part of a particular Unicode script or block.

Mode Modifiers

Change matching modes such as “case insensitive” for specific parts of the regular expression.

Atomic Grouping and Possessive Quantifiers

Nested quantifiers can cause an exponentially increasing amount of backtracking that brings the regex engine to a grinding halt. Atomic grouping and possessive quantifiers provide a solution.

Lookaround with Zero-Length Assertions, part 1 and part 2

With lookahead and lookbehind, collectively called lookaround, you can find matches that are followed or not followed by certain text, and preceded or not preceded by certain text, without having the preceding or following text included in the overall regex match. You can also use lookaround to test the same part of the match for multiple requirements.

Keep The Text Matched So Far out of The Overall Regex Match

Keeping the text matched so far out of the overall regex match allows you to find matches that are preceded by certain text, without having that preceding text included in the overall regex match. This method is primarily of interest with regex flavors that have no or limited support for lookbehind.

Conditionals

A conditional is a special construct that first evaluates a lookaround or backreference, and then execute one sub-regex if the lookaround succeeds, and another sub-regex if the lookaround fails.

Recursion

Recursion matches the whole regex again at a particular point inside the regex, which makes it possible to match balanced constructs.

Subroutine Calls

Subroutine calls allow you to write regular expressions that match the same constructs in multiple places without having to duplicate parts of your regular expression.

Recursion, Subroutines, & Capturing

Capturing groups inside recursion and subroutine calls are handled differently by the regex flavors that support them.

Backreferences with Recursion Level

Special backreferences match the text stored by a capturing group at a particular recursion level, instead of the text most recently matched by that capturing group.

Recursion, Subroutines, & Backtracking

The regex flavors that support recursion and subroutine calls backtrack differently after a recursion or subroutine call fails.

POSIX Bracket Expressions

If you are using a POSIX-compliant regular expression engine, you can use POSIX bracket expressions to match locale-dependent characters.

Issues with Zero-Length Matches

When a regex can find zero-length matches, regex engines use different strategies to avoid getting stuck on a zero-length match when you want to iterate over all matches in a string. This may lead to different match results.

Continuing from The Previous Match Attempt

Forcing a regex match to start at the end of a previous match provides an efficient way to parse text data.

©2015-2025 艾丽卡 support@alaica.com