正则表达式教程 - 学习如何使用正则表达式并发挥最大效用正则表达式教程 - 学习如何使用正则表达式并发挥最大效用正则表达式教程 - 学习如何使用正则表达式并发挥最大效用正则表达式教程 - 学习如何使用正则表达式并发挥最大效用
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
正则表达式教程 - 学习如何使用正则表达式并发挥最大效用
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式教程

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

正则表达式教程
学习如何使用正则表达式并发挥最大效用

本教学教导您所有需要知道的知识,让您能够制作强大且省时的正则表达式。它从最基本的观念开始,因此即使您对正则表达式完全不了解,也能够遵循本教学。

本教学不仅止于此。它还会说明正则表达式引擎的内部运作方式,并提醒您后果。这有助于您快速了解为什么特定正则表达式无法运行您最初预期的动作。当您需要撰写更复杂的正则表达式时,它将为您省去许多猜测和苦恼。

正则表达式究竟是什么 - 术语

基本上,正则表达式是一个描述一定数量文本的模式。它们的名称来自于它们所根据的数学理论。但我们不会深入探讨。你通常会发现这个名称缩写为「regex」或「regexp」。本教学使用「regex」,因为很容易发音复数形式的「regexes」。在本网站上,正则表达式以灰色阴影显示为 regex。

这个第一个范例实际上是一个完全有效的 regex。它是基本模式,只比对文本 regex。一个「比对」是文本片段,或 regex 处理软件发现与模式对应的字节或字符串行。比对在本网站上以蓝色突显。

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b 是一个更复杂的模式。它描述一系列字母、数字、点、底线、百分比符号和连字号,后面跟着一个 at 符号,再后面跟着另一系列字母、数字和连字号,最后跟着一个点和两个或更多字母。换句话说:这个模式描述一个 电子邮件地址。这也显示了本网站套用在正则表达式上的语法突显。字词边界和量词是蓝色,字符类别是橘色,而转义字符是灰色。你会在教学的后续看到其他颜色,例如用于分组的绿色和用于元标记的紫色。

使用上述正则表达式模式,你可以搜索文本文件以寻找电子邮件地址,或验证给定的字符串是否看起来像电子邮件地址。本教学使用术语「字符串」来表示套用正则表达式的文本。本网站以 绿色 突显它们。程序员使用术语「字符串」或「字符字符串」来表示一系列字符。实际上,你可以使用正则表达式搭配任何你可以使用你正在使用的应用程序或编程语言访问的数据。

不同的正则表达式引擎

正则表达式「引擎」是一种可以处理正则表达式的软件,尝试将样式与给定的字符串配对。通常,引擎是较大型应用程序的一部分,您无法直接访问引擎。反而是应用程序在需要时为您调用引擎,确保将正确的正则表达式套用至正确的文件或数据。

在软件世界中,不同的正则表达式引擎通常无法完全兼容。特定引擎的语法和行为称为正则表达式风格。本教学包含所有流行的正则表达式风格,包括 Perl、PCRE、PHP、.NET、Java、JavaScript、XRegExp、VBScript、Python、Ruby、Delphi、R、Tcl、POSIX 以及 许多其他。当这些风格需要不同的语法或展现不同的行为时,本教学会提醒您。即使您的应用程序并未明确包含在本教学中,它也可能使用本教学中涵盖的正则表达式风格,因为大多数应用程序都是使用刚才提到的其中一种编程环境或正则表达式函数库开发的。

初次尝试正则表达式

您可以在支持正则表达式的文本编辑器中轻松自行尝试下列范例。

现在尝试使用 regex reg(ular expressions?|ex(p|es)?) 进行搜索。此 regex 会寻找我在此页面中用来表示「regex」的所有名称(单数和复数)。如果我们只有纯文本搜索,我们需要进行 5 次搜索。使用 regex,我们只需要进行一次搜索。Regex 可以节省您的时间。

如果您是编程人员,您的软件运行速度会更快,因为即使是套用上述 regex 一次的简单 regex 引擎,其性能也会优于最先进的纯文本搜索算法,后者必须在数据中搜索五次。正则表达式也会减少开发时间。使用 regex 引擎,只需一行(例如在 Perl、PHP、Python、Ruby、Java 或 .NET 中)或几行(例如在使用 PCRE 的 C 中)的代码,即可检查用户的输入是否看起来像 有效的电子邮件地址。

正则表达式教程目录

正規表示式教學 - 學習如何使用正規表示式並發揮最大效用
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式教學

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

正規表示式教學
學習如何使用正規表示式並發揮最大效用

本教學教導您所有需要知道的知識,讓您能夠製作強大且省時的正規表示式。它從最基本的觀念開始,因此即使您對正規表示式完全不了解,也能夠遵循本教學。

本教學不僅止於此。它還會說明正規表示式引擎的內部運作方式,並提醒您後果。這有助於您快速了解為什麼特定正規表示式無法執行您最初預期的動作。當您需要撰寫更複雜的正規表示式時,它將為您省去許多猜測和苦惱。

正規表示式究竟是什麼 - 術語

基本上,正規表示式是一個描述一定數量文字的模式。它們的名稱來自於它們所根據的數學理論。但我們不會深入探討。你通常會發現這個名稱縮寫為「regex」或「regexp」。本教學使用「regex」,因為很容易發音複數形式的「regexes」。在本網站上,正規表示式以灰色陰影顯示為 regex。

這個第一個範例實際上是一個完全有效的 regex。它是基本模式,只比對文字 regex。一個「比對」是文字片段,或 regex 處理軟體發現與模式對應的位元組或字元序列。比對在本網站上以藍色突顯。

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b 是一個更複雜的模式。它描述一系列字母、數字、點、底線、百分比符號和連字號,後面跟著一個 at 符號,再後面跟著另一系列字母、數字和連字號,最後跟著一個點和兩個或更多字母。換句話說:這個模式描述一個 電子郵件地址。這也顯示了本網站套用在正規表示式上的語法突顯。字詞邊界和量詞是藍色,字元類別是橘色,而跳脫字元是灰色。你會在教學的後續看到其他顏色,例如用於分組的綠色和用於元標記的紫色。

使用上述正規表示式模式,你可以搜尋文字檔案以尋找電子郵件地址,或驗證給定的字串是否看起來像電子郵件地址。本教學使用術語「字串」來表示套用正規表示式的文字。本網站以 綠色 突顯它們。程式設計師使用術語「字串」或「字元字串」來表示一系列字元。實際上,你可以使用正規表示式搭配任何你可以使用你正在使用的應用程式或程式語言存取的資料。

不同的正規表示式引擎

正規表示式「引擎」是一種可以處理正規表示式的軟體,嘗試將樣式與給定的字串配對。通常,引擎是較大型應用程式的一部分,您無法直接存取引擎。反而是應用程式在需要時為您呼叫引擎,確保將正確的正規表示式套用至正確的檔案或資料。

在軟體世界中,不同的正規表示式引擎通常無法完全相容。特定引擎的語法和行為稱為正規表示式風格。本教學包含所有流行的正規表示式風格,包括 Perl、PCRE、PHP、.NET、Java、JavaScript、XRegExp、VBScript、Python、Ruby、Delphi、R、Tcl、POSIX 以及 許多其他。當這些風格需要不同的語法或展現不同的行為時,本教學會提醒您。即使您的應用程式並未明確包含在本教學中,它也可能使用本教學中涵蓋的正規表示式風格,因為大多數應用程式都是使用剛才提到的其中一種程式設計環境或正規表示式函式庫開發的。

初次嘗試正規表示式

您可以在支援正規表示式的文字編輯器中輕鬆自行嘗試下列範例。

現在嘗試使用 regex reg(ular expressions?|ex(p|es)?) 進行搜尋。此 regex 會尋找我在此頁面中用來表示「regex」的所有名稱(單數和複數)。如果我們只有純文字搜尋,我們需要進行 5 次搜尋。使用 regex,我們只需要進行一次搜尋。Regex 可以節省您的時間。

如果您是程式設計人員,您的軟體執行速度會更快,因為即使是套用上述 regex 一次的簡單 regex 引擎,其效能也會優於最先進的純文字搜尋演算法,後者必須在資料中搜尋五次。正規表示式也會減少開發時間。使用 regex 引擎,只需一行(例如在 Perl、PHP、Python、Ruby、Java 或 .NET 中)或幾行(例如在使用 PCRE 的 C 中)的程式碼,即可檢查使用者的輸入是否看起來像 有效的電子郵件地址。

Regex 教學大綱

Regular Expressions Tutorial - Learn How to Use and Get The Most out of Regular Expressions
  • 简
  • 繁
  • En
About Regular Expressions » Regular Expressions Tutorial

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
Learn How to Use and Get The Most out of Regular Expressions

This tutorial teaches you all you need to know to be able to craft powerful time-saving regular expressions. It starts with the most basic concepts, so that you can follow this tutorial even if you know nothing at all about regular expressions yet.

The tutorial doesn’t stop there. It also explains how a regular expression engine works on the inside and alerts you to the consequences. This helps you to quickly understand why a particular regex does not do what you initially expected. It will save you lots of guesswork and head scratching when you need to write more complex regexes.

What Regular Expressions Are Exactly - Terminology

Basically, a regular expression is a pattern describing a certain amount of text. Their name comes from the mathematical theory on which they are based. But we will not dig into that. You will usually find the name abbreviated to "regex" or "regexp". This tutorial uses "regex", because it is easy to pronounce the plural "regexes". On this website, regular expressions are shaded gray as regex.

This first example is actually a perfectly valid regex. It is the most basic pattern, simply matching the literal text regex. A “match” is the piece of text, or sequence of bytes or characters that pattern was found to correspond to by the regex processing software. Matches are highlighted in blue on this site.

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b is a more complex pattern. It describes a series of letters, digits, dots, underscores, percentage signs and hyphens, followed by an at sign, followed by another series of letters, digits and hyphens, finally followed by a single dot and two or more letters. In other words: this pattern describes an email address. This also shows the syntax highlighting applied to regular expressions on this site. Word boundaries and quantifiers are blue, character classes are orange, and escaped literals are gray. You’ll see additional colors like green for grouping and purple for meta tokens later in the tutorial.

With the above regular expression pattern, you can search through a text file to find email addresses, or verify if a given string looks like an email address. This tutorial uses the term “string” to indicate the text that the regular expression is applied to. This website highlights them in green. The term “string” or “character string” is used by programmers to indicate a sequence of characters. In practice, you can use regular expressions with whatever data you can access using the application or programming language you are working with.

Different Regular Expression Engines

A regular expression “engine” is a piece of software that can process regular expressions, trying to match the pattern to the given string. Usually, the engine is part of a larger application and you do not access the engine directly. Rather, the application invokes it for you when needed, making sure the right regular expression is applied to the right file or data.

As usual in the software world, different regular expression engines are not fully compatible with each other. The syntax and behavior of a particular engine is called a regular expression flavor. This tutorial covers all the popular regular expression flavors, including Perl, PCRE, PHP, .NET, Java, JavaScript, XRegExp, VBScript, Python, Ruby, Delphi, R, Tcl, POSIX, and many others. The tutorial alerts you when these flavors require different syntax or show different behavior. Even if your application is not explicitly covered by the tutorial, it likely uses a regex flavor that is covered, as most applications are developed using one of the programming environments or regex libraries just mentioned.

Give Regexes a First Try

You can easily try the following yourself in a text editor that supports regular expressions.

Now try to search using the regex reg(ular expressions?|ex(p|es)?). This regex finds all names, singular and plural, I have used on this page to say “regex”. If we only had plain text search, we would have needed 5 searches. With regexes, we need just one search. Regexes save you time.

If you are a programmer, your software will run faster since even a simple regex engine applying the above regex once will outperform a state of the art plain text search algorithm searching through the data five times. Regular expressions also reduce development time. With a regex engine, it takes only one line (e.g. in Perl, PHP, Python, Ruby, Java, or .NET) or a couple of lines (e.g. in C using PCRE) of code to, say, check if the user’s input looks like a valid email address.

Regex Tutorial Table of Contents

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