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

加载更多搜索结果...

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

欢迎
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

关于正则表达式

正则表达式(简称 regex 或 regexp)是一种用于描述搜索模式的特殊文本字符串。您可以将正则表达式想成是加强版的通配符。您可能熟悉通配符表示法,例如 *.txt,用于在文件管理员中寻找所有文本文件。等效的正则表达式是 ^.*\.txt$。

但您使用正则表达式可以做更多的事。在支持正则表达式的文本编辑器或专业文本处理工具中,您可以使用正则表达式 \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b 来搜索电子邮件地址。确切来说,是任何电子邮件地址。程序员可以使用非常类似的正则表达式(将第一个 \b 替换为 ^,将最后一个替换为 $)来检查用户是否输入了 格式正确的电子邮件地址。只要一行代码,无论该代码是用 Perl、PHP、Java、.NET 语言 或许多其他语言撰写的。

正则表达式快速入门

如果您只是想用正则表达式试试水温,请参阅一页式正则表达式快速入门。虽然您无法从这个简要的概述中学会有效使用正则表达式,但足以让您能够拼凑出一堆简单的正则表达式。快速入门中的每个部分都直接链接到教程中的详细信息。

完整的正则表达式教程

如果您对上述范例或快速入门感到一头雾水,请别担心。任何非平凡的正则表达式对不熟悉它们的人来说看起来都令人望而生畏。但是只要有一点经验,您很快就能够撰写自己的正则表达式,就像您从未做过其他事情一样。正则表达式教程会逐一说明所有内容。

本教程非常独特,因为它不仅说明正则表达式语法,还详细说明正则表达式引擎实际如何运行其工作。即使您已经使用正则表达式一段时间,您仍会学到很多东西。这将有助于您快速了解特定正则表达式为何无法运行您最初预期的动作,让您在撰写更复杂的正则表达式时省去许多猜测和苦恼。

替换字符串教程

替换字符串,也称为替换文本,是搜索和替换期间每个正则表达式比对都替换成的文本。在大多数应用程序中,替换文本支持特殊语法,让您可以在替换中重复使用正则表达式比对的文本或其部分。本网站还包含一个完整的替换字符串教程,说明此语法。虽然与正则表达式相比,替换字符串相当简单,但各种应用程序使用的语法及其实际行为之间仍然有很大的差异。

支持正则表达式的应用程序和语言

有许多软件应用程序和编程语言支持正则表达式。如果您是程序员,您可以省下大量时间和精力。您通常可以用一或几行代码中的单一正则表达式来完成原本需要数十或数百行的动作。

许多应用程序和编程语言都有自己的正则表达式实作,通常与其他实作有细微的差异,有时甚至有显著的差异。当两个应用程序使用不同的正则表达式实作时,我们会说它们使用不同的「正则表达式风格」。与大多数其他正则表达式教程不同,本网站上的教程涵盖所有流行的正则表达式风格,并指出您应该注意的差异。

不只适用于程序员

如果您不是程序员,您也可以在许多情况下使用正则表达式。它们让寻找信息变得容易许多。您可以在强大的搜索和取代作业中使用它们,以快速变更大量文件。一个简单的范例是gr[ae]y,它在一个作业中找到 gray 这个字的两种拼法,而非两个。有许多文本编辑器和搜索和取代工具具有不错的正则表达式支持。

關於正規表示式
  • 简
  • 繁
  • En
關於正規表示式

歡迎
Regular Expressions 快速開始
Regular Expressions 教學
替換字串教學
應用程式和語言
Regular Expressions 範例
Regular Expressions 參考
替換字串參考

關於正規表示式

正規表示式(簡稱 regex 或 regexp)是一種用於描述搜尋模式的特殊文字字串。您可以將正規表示式想成是加強版的萬用字元。您可能熟悉萬用字元表示法,例如 *.txt,用於在檔案管理員中尋找所有文字檔案。等效的正規表示式是 ^.*\.txt$。

但您使用正規表示式可以做更多的事。在支持正規表示式的文字編輯器或專業文字處理工具中,您可以使用正規表示式 \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b 來搜尋電子郵件地址。確切來說,是任何電子郵件地址。程式設計師可以使用非常類似的正規表示式(將第一個 \b 替換為 ^,將最後一個替換為 $)來檢查使用者是否輸入了 格式正確的電子郵件地址。只要一行程式碼,無論該程式碼是用 Perl、PHP、Java、.NET 語言 或許多其他語言撰寫的。

Regular Expressions 快速開始

如果您只是想用正規表示式試試水溫,請參閱一頁式正規表示式快速入門。雖然您無法從這個簡要的概述中學會有效使用正規表示式,但足以讓您能夠拼湊出一堆簡單的正規表示式。快速入門中的每個部分都直接連結到教學課程中的詳細資訊。

完整的正規表示式教學課程

如果您對上述範例或快速入門感到一頭霧水,請別擔心。任何非平凡的正規表示式對不熟悉它們的人來說看起來都令人望而生畏。但是只要有一點經驗,您很快就能夠撰寫自己的正規表示式,就像您從未做過其他事情一樣。正規表示式教學課程會逐一說明所有內容。

本教學課程非常獨特,因為它不僅說明正規表示式語法,還詳細說明正規表示式引擎實際如何執行其工作。即使您已經使用正規表示式一段時間,您仍會學到很多東西。這將有助於您快速了解特定正規表示式為何無法執行您最初預期的動作,讓您在撰寫更複雜的正規表示式時省去許多猜測和苦惱。

替換字串教學

替換字串,也稱為替換文字,是搜尋和替換期間每個正規表示式比對都替換成的文字。在大多數應用程式中,替換文字支援特殊語法,讓您可以在替換中重複使用正規表示式比對的文字或其部分。本網站還包含一個完整的替換字串教學課程,說明此語法。雖然與正規表示式相比,替換字串相當簡單,但各種應用程式使用的語法及其實際行為之間仍然有很大的差異。

支援正規表示式的應用程式和語言

有許多軟體應用程式和程式設計語言支援正規表示式。如果您是程式設計師,您可以省下大量時間和精力。您通常可以用一或幾行程式碼中的單一正規表示式來完成原本需要數十或數百行的動作。

許多應用程式和程式設計語言都有自己的正規表示式實作,通常與其他實作有細微的差異,有時甚至有顯著的差異。當兩個應用程式使用不同的正規表示式實作時,我們會說它們使用不同的「正規表示式風格」。與大多數其他正規表示式教學課程不同,本網站上的教學課程涵蓋所有流行的正規表示式風格,並指出您應該注意的差異。

不只適用於程式設計師

如果您不是程式設計師,您也可以在許多情況下使用正規表示式。它們讓尋找資訊變得容易許多。您可以在強大的搜尋和取代作業中使用它們,以快速變更大量檔案。一個簡單的範例是gr[ae]y,它在一個作業中找到 gray 這個字的兩種拼法,而非兩個。有許多文字編輯器和搜尋和取代工具具有不錯的正規表示式支援。

About Regular Expressions
  • 简
  • 繁
  • En
About Regular Expressions

Welcome
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

About Regular Expressions

A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is ^.*\.txt$.

But you can do much more with regular expressions. In a text editor or a specialized text processing tool with regular expression support, you could use the regular expression \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b to search for an email address. Any email address, to be exact. A very similar regular expression (replace the first \b with ^ and the last one with $) can be used by a programmer to check whether the user entered a properly formatted email address. In just one line of code, whether that code is written in Perl, PHP, Java, a .NET language, or a multitude of other languages.

Regular Expressions Quick Start

If you just want to get your feet wet with regular expressions, take a look at the one-page regular expressions quick start. While you can’t learn to efficiently use regular expressions from this brief overview, it’s enough to be able to throw together a bunch of simple regular expressions. Each section in the quick start links directly to detailed information in the tutorial.

Complete Regular Expressions Tutorial

Do not worry if the above example or the quick start make little sense to you. Any non-trivial regex looks daunting to anybody not familiar with them. But with just a bit of experience, you will soon be able to craft your own regular expressions like you have never done anything else. The Regular Expressions Tutorial explains everything bit by bit.

This tutorial is quite unique because it not only explains the regex syntax, but also describes in detail how the regex engine actually goes about its work. You will learn quite a lot, even if you have already been using regular expressions for some time. This will help you to understand quickly why a particular regex does not do what you initially expected, saving you lots of guesswork and head scratching when writing more complex regexes.

Replacement Strings Tutorial

A replacement string, also known as the replacement text, is the text that each regular expression match is replaced with during a search-and-replace. In most applications, the replacement text supports special syntax that allows you to reuse the text matched by the regular expression or parts thereof in the replacement. This website also includes a complete replacement strings tutorial that explains this syntax. While replacement strings are fairly simple compared with regular expressions, there is still great variety between the syntax used by various applications and their actual behavior.

Applications & Languages That Support Regexes

There are many software applications and programming languages that support regular expressions. If you are a programmer, you can save yourself lots of time and effort. You can often accomplish with a single regular expression in one or a few lines of code what would otherwise take dozens or hundreds.

Many applications and programming languages have their own implementation of regular expressions, often with slight and sometimes with significant differences from other implementations. When two applications use a different implementation of regular expressions, we say that they use different “regular expression flavors”. Unlike most other regex tutorials, the tutorial on this website covers all the popular regular expression flavors, and indicates the differences that you should watch out for.

Not Only for Programmers

If you are not a programmer, you can use regular expressions in many situations just as well. They make finding information a lot easier. You can use them in powerful search and replace operations to quickly make changes across large numbers of files. A simple example is gr[ae]y which finds both spellings of the word gray in one operation, instead of two. There are many text editors and search and replace tools with decent regex support.

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