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

加载更多搜索结果...

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

范例
正则表达式范例
数字范围
浮点数
电子邮件地址
IP 地址
有效日期
数字日期转换为文本
信用卡号码
比对完整行
删除重复行
编程
两个相邻的字词
陷阱
灾难性回溯
过多重复
拒绝服务
让所有内容都变成选用
重复捕获组
混合 Unicode 和 8 比特
更多内容
简介
正则表达式快速入门
正则表达式教程
取代字符串教学
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

正则表达式范例

以下,您将找到许多范例模式,供您使用并调整为自己的目的。制作每个正则表达式的关键技术都有说明,并附上链接至 教学 中对应的页面,其中会详细说明这些概念和技术。

喔,而且您绝对不需要是程序员才能利用正则表达式!

截取 HTML 标签

<TAG\b[^>]*>(.*?)</TAG> 比对特定 HTML 标签的打开和关闭配对。标签之间的任何内容都会截取到第一个 反向参考 中。正则表达式中的问号会让星号 变为非贪婪,以确保它在第一个关闭标签之前停止,而不是像贪婪星号一样在最后一个关闭标签之前停止。这个正则表达式无法正确比对嵌套标签,例如 <TAG>one<TAG>two</TAG>one</TAG>。

<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1> 将配对任何 HTML 标签的打开和关闭配对。请务必关闭大小写敏感性。此解决方案的关键在于使用 反向引用 \1 在 regex 中。标签之间的任何内容都会截取到第二个反向引用中。此解决方案也不会配对嵌套在自身中的标签。

修剪空白

您可以通过运行 regex 搜索和取代,轻松修剪字符串或文本档中各行开头和结尾不必要的空白。搜索 ^[ \t]+ 并以空白取代,以删除前导空白(空格和 tab)。搜索 [ \t]+$ 以修剪尾随空白。通过将正则表达式 合并 为 ^[ \t]+|[ \t]+$ 来运行这两个动作。您可以将 [ \t](配对空格或 tab)扩充为 [ \t\r\n] 的 字符类别,如果您也想要移除换行。或者,您可以改用 简写 \s。

更详细的范例

数字范围。由于正则表达式使用文本而非数字,因此配对特定的数字范围需要特别小心。

配对浮点数。也说明了在正则表达式中将所有内容设为选配的常见错误。

配对电子邮件地址。对于什么是配对电子邮件地址的适当 regex,存在许多争议。这是一个完美的范例,显示您需要确切知道您尝试配对的内容(以及不配对的内容),并且 regex 的复杂性和准确性之间总是存在权衡。

配对 IP 地址.

比对有效日期。一个正则表达式,比对 31-12-1999,但不会比对 31-13-1999。

寻找或验证信用卡号码。验证在订单表单中输入的信用卡号码。在文档中寻找信用卡号码,以进行安全性稽核。

比对完整行。显示如何比对文本文件中的完整行,而不仅仅是满足特定需求的行部分。此外,还显示如何比对特定正则表达式不比对的行。

移除重复行或项目。说明如何简单但灵活地使用截取括弧或反向引用。

处理原代码的正则表达式范例。如何比对常见的编程语言语法,例如注解、字符串、数字等。

两个单字相邻。显示如何使用正则表达式仿真某些工具具有的「相邻」操作符。

常见陷阱

灾难性回溯。如果正则表达式似乎需要花费很长时间,或只是让应用程序当机,它很可能已经发生灾难性回溯。解决方案通常是更具体地说明要比对的内容,因此引擎必须尝试的比对次数不会呈指数上升。

让所有内容都可选。如果正则表达式中的所有部分都是可选的,它将比对任何位置的零长度字符串。正则表达式需要表达不同的部分根据存在的部分而可选的事实。

重复捕获组与截取重复群组。重复捕获组只会捕获组的最后一个反复。如果你想要截取所有反复,请截取重复群组。

混合 Unicode 和 8 比特字符码。在 Unicode 引擎和主旨字符串中使用 8 比特字符码,例如 \x80,可能会产生意外的结果。

正規表示式範例
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例

範例
正規表示式範例
數字範圍
浮點數
電子郵件地址
IP 位址
有效日期
數字日期轉換為文字
信用卡號碼
比對完整行
刪除重複行
程式設計
兩個相鄰的字詞
陷阱
災難性回溯
過多重複
拒絕服務
讓所有內容都變成選用
重複擷取群組
混合 Unicode 和 8 位元
本網站的更多資訊
簡介
正規表示式快速入門
正規表示式教學
取代字串教學
應用程式和語言
正規表示式範例
正規表示式參考
取代字串參考

正規表示式範例

以下,您將找到許多範例模式,供您使用並調整為自己的目的。製作每個正規表示式的關鍵技術都有說明,並附上連結至 教學 中對應的頁面,其中會詳細說明這些概念和技術。

喔,而且您絕對不需要是程式設計師才能利用正規表示式!

擷取 HTML 標籤

<TAG\b[^>]*>(.*?)</TAG> 比對特定 HTML 標籤的開啟和關閉配對。標籤之間的任何內容都會擷取到第一個 反向參考 中。正規表示式中的問號會讓星號 變為非貪婪,以確保它在第一個關閉標籤之前停止,而不是像貪婪星號一樣在最後一個關閉標籤之前停止。這個正規表示式無法正確比對巢狀標籤,例如 <TAG>one<TAG>two</TAG>one</TAG>。

<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1> 將配對任何 HTML 標籤的開啟和關閉配對。請務必關閉大小寫敏感性。此解決方案的關鍵在於使用 反向參照 \1 在 regex 中。標籤之間的任何內容都會擷取到第二個反向參照中。此解決方案也不會配對嵌套在自身中的標籤。

修剪空白

您可以透過執行 regex 搜尋和取代,輕鬆修剪字串或文字檔中各行開頭和結尾不必要的空白。搜尋 ^[ \t]+ 並以空白取代,以刪除前導空白(空格和 tab)。搜尋 [ \t]+$ 以修剪尾隨空白。透過將正規表示式 合併 為 ^[ \t]+|[ \t]+$ 來執行這兩個動作。您可以將 [ \t](配對空格或 tab)擴充為 [ \t\r\n] 的 字元類別,如果您也想要移除換行。或者,您可以改用 簡寫 \s。

更詳細的範例

數字範圍。由於正規表示式使用文字而非數字,因此配對特定的數字範圍需要特別小心。

配對浮點數。也說明了在正規表示式中將所有內容設為選配的常見錯誤。

配對電子郵件地址。對於什麼是配對電子郵件地址的適當 regex,存在許多爭議。這是一個完美的範例,顯示您需要確切知道您嘗試配對的內容(以及不配對的內容),並且 regex 的複雜性和準確性之間總是存在權衡。

配對 IP 位址.

比對有效日期。一個正規表示式,比對 31-12-1999,但不會比對 31-13-1999。

尋找或驗證信用卡號碼。驗證在訂單表單中輸入的信用卡號碼。在文件中尋找信用卡號碼,以進行安全性稽核。

比對完整行。顯示如何比對文字檔案中的完整行,而不仅仅是滿足特定需求的行部分。此外,還顯示如何比對特定正規表示式不比對的行。

移除重複行或項目。說明如何簡單但靈活地使用擷取括弧或反向參照。

處理原始碼的正規表示式範例。如何比對常見的程式語言語法,例如註解、字串、數字等。

兩個單字相鄰。顯示如何使用正規表示式模擬某些工具具有的「相鄰」運算子。

常見陷阱

災難性回溯。如果正規表示式似乎需要花費很長時間,或只是讓應用程式當機,它很可能已經發生災難性回溯。解決方案通常是更具體地說明要比對的內容,因此引擎必須嘗試的比對次數不會呈指數上升。

讓所有內容都可選。如果正規表示式中的所有部分都是可選的,它將比對任何位置的零長度字串。正規表示式需要表達不同的部分根據存在的部分而可選的事實。

重複擷取群組與擷取重複群組。重複擷取群組只會擷取群組的最後一個反覆。如果你想要擷取所有反覆,請擷取重複群組。

混合 Unicode 和 8 位元字元碼。在 Unicode 引擎和主旨字串中使用 8 位元字元碼,例如 \x80,可能會產生意外的結果。

Sample Regular Expressions
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions

Examples
Regular Expressions Examples
Numeric Ranges
Floating Point Numbers
Email Addresses
IP Addresses
Valid Dates
Numeric Dates to Text
Credit Card Numbers
Matching Complete Lines
Deleting Duplicate Lines
Programming
Two Near Words
Pitfalls
Catastrophic Backtracking
Too Many Repetitions
Denial of Service
Making Everything Optional
Repeated Capturing Group
Mixing Unicode & 8-bit
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

Sample Regular Expressions

Below, you will find many example patterns that you can use for and adapt to your own purposes. Key techniques used in crafting each regex are explained, with links to the corresponding pages in the tutorial where these concepts and techniques are explained in great detail.

Oh, and you definitely do not need to be a programmer to take advantage of regular expressions!

Grabbing HTML Tags

<TAG\b[^>]*>(.*?)</TAG> matches the opening and closing pair of a specific HTML tag. Anything between the tags is captured into the first backreference. The question mark in the regex makes the star lazy, to make sure it stops before the first closing tag rather than before the last, like a greedy star would do. This regex will not properly match tags nested inside themselves, like in <TAG>one<TAG>two</TAG>one</TAG>.

<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1> will match the opening and closing pair of any HTML tag. Be sure to turn off case sensitivity. The key in this solution is the use of the backreference \1 in the regex. Anything between the tags is captured into the second backreference. This solution will also not match tags nested in themselves.

Trimming Whitespace

You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t]+$ to trim trailing whitespace. Do both by combining the regular expressions into ^[ \t]+|[ \t]+$. Instead of [ \t] which matches a space or a tab, you can expand the character class into [ \t\r\n] if you also want to strip line breaks. Or you can use the shorthand \s instead.

More Detailed Examples

Numeric Ranges. Since regular expressions work with text rather than numbers, matching specific numeric ranges requires a bit of extra care.

Matching a Floating Point Number. Also illustrates the common mistake of making everything in a regular expression optional.

Matching an Email Address. There’s a lot of controversy about what is a proper regex to match email addresses. It’s a perfect example showing that you need to know exactly what you’re trying to match (and what not), and that there’s always a trade-off between regex complexity and accuracy.

Matching an IP Address.

Matching Valid Dates. A regular expression that matches 31-12-1999 but not 31-13-1999.

Finding or Verifying Credit Card Numbers. Validate credit card numbers entered on your order form. Find credit card numbers in documents for a security audit.

Matching Complete Lines. Shows how to match complete lines in a text file rather than just the part of the line that satisfies a certain requirement. Also shows how to match lines in which a particular regex does not match.

Removing Duplicate Lines or Items. Illustrates simple yet clever use of capturing parentheses or backreferences.

Regex Examples for Processing Source Code. How to match common programming language syntax such as comments, strings, numbers, etc.

Two Words Near Each Other. Shows how to use a regular expression to emulate the “near” operator that some tools have.

Common Pitfalls

Catastrophic Backtracking. If your regular expression seems to take forever, or simply crashes your application, it has likely contracted a case of catastrophic backtracking. The solution is usually to be more specific about what you want to match, so the number of matches the engine has to try doesn’t rise exponentially.

Making Everything Optional. If all the parts in your regex are optional, it will match a zero-length string anywhere. Your regex will need to express the facts that different parts are optional depending on which parts are present.

Repeating a Capturing Group vs. Capturing a Repeated Group. Repeating a capturing group will capture only the last iteration of the group. Capture a repeated group if you want to capture all iterations.

Mixing Unicode and 8-bit Character Codes. Using 8-bit character codes like \x80 with a Unicode engine and subject string may give unexpected results.

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