{"id":13490159,"url":"https://github.com/cdoco/learn-regex-zh","last_synced_at":"2026-02-24T09:03:29.060Z","repository":{"id":37318604,"uuid":"98608318","full_name":"cdoco/learn-regex-zh","owner":"cdoco","description":":cn: 翻译: 学习正则表达式的简单方法","archived":false,"fork":false,"pushed_at":"2022-03-12T15:01:00.000Z","size":22,"stargazers_count":2628,"open_issues_count":6,"forks_count":475,"subscribers_count":54,"default_branch":"master","last_synced_at":"2025-03-26T15:24:35.385Z","etag":null,"topics":["chinese-translation","learn-regex","learn-regex-zh","regex","regexp"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cdoco.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-28T04:29:55.000Z","updated_at":"2025-03-26T10:14:16.000Z","dependencies_parsed_at":"2022-07-12T12:24:56.014Z","dependency_job_id":null,"html_url":"https://github.com/cdoco/learn-regex-zh","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cdoco/learn-regex-zh","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Flearn-regex-zh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Flearn-regex-zh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Flearn-regex-zh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Flearn-regex-zh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdoco","download_url":"https://codeload.github.com/cdoco/learn-regex-zh/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdoco%2Flearn-regex-zh/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29777610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-24T04:54:30.205Z","status":"ssl_error","status_checked_at":"2026-02-24T04:53:58.628Z","response_time":75,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chinese-translation","learn-regex","learn-regex-zh","regex","regexp"],"created_at":"2024-07-31T19:00:41.842Z","updated_at":"2026-02-24T09:03:29.026Z","avatar_url":"https://github.com/cdoco.png","language":null,"readme":"\u003cbr/\u003e\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://i.imgur.com/bYwl7Vf.png\" alt=\"Learn Regex\"\u003e\n\u003c/p\u003e\u003cbr/\u003e\n\n## 什么是正则表达式 ？\n\n\u003e 正则表达式是一种被用于从文本中检索符合某些特定模式的文本。\n\n正则表达式是从左到右来匹配一个字符串的。“Regular Expression”这个词太长了，我们通常使用它的缩写“regex”或者“regexp”。\n正则表达式可以被用来替换字符串中的文本、验证表单、基于模式匹配从一个字符串中提取字符串等等。\n\u003cbr /\u003e\n\n想象一下，您正在编写应用程序，并且您希望在用户选择用户名时设置规则。我们希望用户名可以包含字母，数字，下划线和连字符。\n为了让它看起来不丑，我们还想限制用户名中的字符数量。这时我们可以使用以下正则表达式来验证用户名：\n\n\u003cp align=\"center\"\u003e\n\u003cimg src=\"https://i.imgur.com/UrDb9qc.png\" alt=\"Regular expression\"\u003e\n\u003c/p\u003e\n\n上面这个正则表达式可以匹配 `john_doe`，`jo-hn_doe` 和 `john12_as`。但是它不能匹配 `Jo`，因为该字符串里面包含大写字符，并且它太短了。\n\n## 目录\n\n- [基本匹配](#1-基本匹配)\n- [元字符](#2-元字符)\n  - [英文句号](#21-英文句号)\n  - [字符集](#22-字符集)\n    - [否定字符集](#221-否定字符集)\n  - [重复](#23-重复)\n    - [星号](#231-星号)\n    - [加号](#232-加号)\n    - [问号](#233-问号)\n  - [花括号](#24-花括号)\n  - [字符组](#25-字符组)\n  - [分支结构](#26-分支结构)\n  - [转义特殊字符](#27-转义特殊字符)\n  - [定位符](#28-定位符)\n    - [插入符号](#281-插入符号)\n    - [美元符号](#282-美元符号)\n- [简写字符集](#3-简写字符集)\n- [断言](#4-断言)\n  - [正向先行断言](#41-正向先行断言)\n  - [负向先行断言](#42-负向先行断言)\n  - [正向后行断言](#43-正向后行断言)\n  - [负向后行断言](#44-负向后行断言)\n- [标记](#5-标记)\n  - [不区分大小写](#51-不区分大小写)\n  - [全局搜索](#52-全局搜索)\n  - [多行匹配](#53-多行匹配)\n- [常用正则表达式](#常用正则表达式)\n\n## 1. 基本匹配\n\n正则表达式只是我们用于在文本中检索字符串的模式。例如正则表达式 `cat`，表示：字母 `c` 后面跟着一个字母 `a`，再后面跟着一个字母 `t`。\n\n\u003cpre\u003e\n\"cat\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e sat on the mat\n\u003c/pre\u003e\n\n正则表达式 `123` 会匹配字符串“123”。通过将正则表达式中的每个字符逐个与要匹配的字符串中的每个字符进行比较，来完成正则匹配。\n正则表达式通常区分大小写，因此正则表达式 `Cat` 与字符串“cat”不匹配。\n\n\u003cpre\u003e\n\"Cat\" =\u003e The cat sat on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eCat\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n## 2. 元字符\n\n元字符是正则表达式的基本组成元素。元字符在这里跟它通常表达的意思不一样，而是以某种特殊的含义去解释。有些元字符在写在方括号内时有特殊含义。\n元字符如下：\n\n|元字符|描述|\n|:----:|----|\n|.|匹配除换行符以外的任意字符。|\n|[ ]|字符类，匹配方括号中包含的任意字符。|\n|[^ ]|否定字符类。匹配方括号中不包含的任意字符|\n|*|匹配前面的子表达式零次或多次|\n|+|匹配前面的子表达式一次或多次|\n|?|匹配前面的子表达式零次或一次，或指明一个非贪婪限定符。|\n|{n,m}|花括号，匹配前面字符至少 n 次，但是不超过 m 次。|\n|(xyz)|字符组，按照确切的顺序匹配字符 xyz。|\n|\u0026#124;|分支结构，匹配符号之前的字符或后面的字符。|\n|\u0026#92;|转义符，它可以还原元字符原来的含义，允许你匹配保留字符 \u003ccode\u003e[ ] ( ) { } . * + ? ^ $ \\ \u0026#124;\u003c/code\u003e|\n|^|匹配行的开始|\n|$|匹配行的结束|\n\n## 2.1 英文句号\n\n英文句号 `.` 是元字符的最简单的例子。元字符 `.` 可以匹配任意单个字符。它不会匹配换行符和新行的字符。例如正则表达式 `.ar`，表示：任意字符后面跟着一个字母 `a`，\n再后面跟着一个字母 `r`。\n\n\u003cpre\u003e\n\".ar\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003epar\u003c/strong\u003e\u003c/a\u003eked in the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egar\u003c/strong\u003e\u003c/a\u003eage.\n\u003c/pre\u003e\n\n## 2.2 字符集\n\n字符集也称为字符类。方括号被用于指定字符集。使用字符集内的连字符来指定字符范围。方括号内的字符范围的顺序并不重要。\n例如正则表达式 `[Tt]he`，表示：大写 `T` 或小写 `t` ，后跟字母 `h`，再后跟字母 `e`。\n\n\u003cpre\u003e\n\"[Tt]he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car parked in \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n然而，字符集中的英文句号表示它字面的含义。正则表达式 `ar[.]`，表示小写字母 `a`，后面跟着一个字母 `r`，再后面跟着一个英文句号 `.` 字符。\n\n\u003cpre\u003e\n\"ar[.]\" =\u003e A garage is a good place to park a c\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ear.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n### 2.2.1 否定字符集\n\n一般来说插入字符 `^` 表示一个字符串的开始，但是当它在方括号内出现时，它会取消字符集。例如正则表达式 `[^c]ar`，表示：除了字母 `c` 以外的任意字符，后面跟着字符 `a`，\n再后面跟着一个字母 `r`。\n\n\u003cpre\u003e\n\"[^c]ar\" =\u003e The car \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003epar\u003c/strong\u003e\u003c/a\u003eked in the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egar\u003c/strong\u003e\u003c/a\u003eage.\n\u003c/pre\u003e\n\n## 2.3 重复\n\n以下元字符 `+`，`*` 或 `?` 用于指定子模式可以出现多少次。这些元字符在不同情况下的作用不同。\n\n### 2.3.1 星号\n\n星号 `*` 表示匹配上一个匹配规则零次或多次。正则表达式 `a*` 表示小写字母 `a` 可以重复零次或者多次。但是它如果出现在字符集或者字符类之后，它表示整个字符集的重复。\n例如正则表达式 `[a-z]*`，表示：一行中可以包含任意数量的小写字母。\n\n\u003cpre\u003e\n\"[a-z]*\" =\u003e T\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ehe\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eparked\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ein\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egarage\u003c/strong\u003e\u003c/a\u003e #21.\n\u003c/pre\u003e\n\n星号 `*` 可以与元符号 `.` 用在一起，用来匹配任意字符串 `.*`。星号 `*` 可以与空格符 `\\s` 一起使用，用来匹配一串空格字符。\n例如正则表达式 `\\s*cat\\s*`，表示：零个或多个空格，后面跟小写字母 `c`，再后面跟小写字母 `a`，再在后面跟小写字母 `t`，后面再跟零个或多个空格。\n\n\u003cpre\u003e\n\"\\s*cat\\s*\" =\u003e The fat\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e cat \u003c/strong\u003e\u003c/a\u003esat on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n### 2.3.2 加号\n\n加号 `+` 表示匹配上一个字符一次或多次。例如正则表达式 `c.+t`，表示：一个小写字母 `c`，后跟任意数量的字符，后跟小写字母 `t`。\n\n\u003cpre\u003e\n\"c.+t\" =\u003e The fat \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat sat on the mat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n### 2.3.3 问号\n\n在正则表达式中，元字符 `?` 用来表示前一个字符是可选的。该符号匹配前一个字符零次或一次。\n例如正则表达式 `[T]?he`，表示：可选的大写字母 `T`，后面跟小写字母 `h`，后跟小写字母 `e`。\n\n\u003cpre\u003e\n\"[T]he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in the garage.\n\u003c/pre\u003e\n\u003cpre\u003e\n\"[T]?he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in t\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ehe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n## 2.4 花括号\n\n在正则表达式中花括号（也被称为量词？）用于指定字符或一组字符可以重复的次数。例如正则表达式 `[0-9]{2,3}`，表示：匹配至少 2 位数字但不超过 3 位（0 到 9 范围内的字符）。\n\n\u003cpre\u003e\n\"[0-9]{2,3}\" =\u003e The number was 9.\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e999\u003c/strong\u003e\u003c/a\u003e7 but we rounded it off to \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e10\u003c/strong\u003e\u003c/a\u003e.0.\n\u003c/pre\u003e\n\n我们可以省略第二个数字。例如正则表达式 `[0-9]{2,}`，表示：匹配 2 个或更多个数字。如果我们也删除逗号，则正则表达式 `[0-9]{2}`，表示：匹配正好为 2 位数的数字。\n\n\u003cpre\u003e\n\"[0-9]{2,}\" =\u003e The number was 9.\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e9997\u003c/strong\u003e\u003c/a\u003e but we rounded it off to \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e10\u003c/strong\u003e\u003c/a\u003e.0.\n\u003c/pre\u003e\n\n\u003cpre\u003e\n\"[0-9]{2}\" =\u003e The number was 9.\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e99\u003c/strong\u003e\u003c/a\u003e\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e97\u003c/strong\u003e\u003c/a\u003e but we rounded it off to \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003e10\u003c/strong\u003e\u003c/a\u003e.0.\n\u003c/pre\u003e\n\n## 2.5 字符组\n\n字符组是一组写在圆括号内的子模式 `(...)`。正如我们在正则表达式中讨论的那样，如果我们把一个量词放在一个字符之后，它会重复前一个字符。\n但是，如果我们把量词放在一个字符组之后，它会重复整个字符组。\n例如正则表达式 `(ab)*` 表示匹配零个或多个的字符串“ab”。我们还可以在字符组中使用元字符 `|`。例如正则表达式 `(c|g|p)ar`，表示：小写字母 `c`、`g` 或 `p` 后面跟字母 `a`，后跟字母 `r`。\n\n\u003cpre\u003e\n\"(c|g|p)ar\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e is \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003epar\u003c/strong\u003e\u003c/a\u003eked in the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003egar\u003c/strong\u003e\u003c/a\u003eage.\n\u003c/pre\u003e\n\n## 2.6 分支结构\n\n在正则表达式中垂直条 `|` 用来定义分支结构，分支结构就像多个表达式之间的条件。现在你可能认为这个字符集和分支结构的工作方式一样。\n但是字符集和分支结构巨大的区别是字符集只在字符级别上有作用，然而分支结构在表达式级别上依然可以使用。\n例如正则表达式 `(T|t)he|car`，表示：匹配大写字母 `T` 或小写字母 `t`，后面跟小写字母 `h`，后跟小写字母 `e`，或匹配小写字母 `c`，后跟小写字母 `a`，后跟小写字母 `r`。\n\n\u003cpre\u003e\n\"(T|t)he|car\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecar\u003c/strong\u003e\u003c/a\u003e is parked in \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n## 2.7 转义特殊字符\n\n正则表达式中使用反斜杠 `\\` 来转义下一个字符。这将允许你使用保留字符来作为匹配字符 `{ } [ ] / \\ + * . $ ^ | ?`。在特殊字符前面加 `\\`，就可以使用它来做匹配字符。\n例如正则表达式 `.` 是用来匹配除了换行符以外的任意字符。现在要在输入字符串中匹配 `.` 字符，正则表达式 `(f|c|m)at\\.?`，表示：小写字母 `f`、`c` 或者 `m` 后跟小写字母 `a`，后跟小写字母 `t`，后跟可选的 `.` 字符。\n\n\u003cpre\u003e\n\"(f|c|m)at\\.?\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e sat on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n## 2.8 定位符\n\n在正则表达式中，为了检查匹配符号是否是起始符号或结尾符号，我们使用定位符。\n定位符有两种类型：第一种类型是 `^` 检查匹配字符是否是起始字符，第二种类型是 `$`，它检查匹配字符是否是输入字符串的最后一个字符。\n\n### 2.8.1 插入符号\n\n插入符号 `^` 符号用于检查匹配字符是否是输入字符串的第一个字符。如果我们使用正则表达式 `^a`（如果 a 是起始符号）匹配字符串 `abc`，它会匹配到 `a`。\n但是如果我们使用正则表达式 `^b`，它是匹配不到任何东西的，因为在字符串 `abc` 中“b”不是起始字符。\n让我们来看看另一个正则表达式 `^(T|t)he`，这表示：大写字母 `T` 或小写字母 `t` 是输入字符串的起始符号，后面跟着小写字母 `h`，后跟小写字母 `e`。\n\n\u003cpre\u003e\n\"(T|t)he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e garage.\n\u003c/pre\u003e\n\n\u003cpre\u003e\n\"^(T|t)he\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e car is parked in the garage.\n\u003c/pre\u003e\n\n### 2.8.2 美元符号\n\n美元 `$` 符号用于检查匹配字符是否是输入字符串的最后一个字符。例如正则表达式 `(at\\.)$`，表示：小写字母 `a`，后跟小写字母 `t`，后跟一个 `.` 字符，且这个匹配器必须是字符串的结尾。\n\n\u003cpre\u003e\n\"(at\\.)\" =\u003e The fat c\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e s\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e on the m\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n\u003cpre\u003e\n\"(at\\.)$\" =\u003e The fat cat sat on the m\u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n## 3. 简写字符集\n\n正则表达式为常用的字符集和常用的正则表达式提供了简写。简写字符集如下：\n\n|简写|描述|\n|:----:|----|\n|.|匹配除换行符以外的任意字符|\n|\\w|匹配所有字母和数字的字符：`[a-zA-Z0-9_]`|\n|\\W|匹配非字母和数字的字符：`[^\\w]`|\n|\\d|匹配数字：`[0-9]`|\n|\\D|匹配非数字：`[^\\d]`|\n|\\s|匹配空格符：`[\\t\\n\\f\\r\\p{Z}]`|\n|\\S|匹配非空格符：`[^\\s]`|\n\n## 4. 断言\n\n后行断言和先行断言有时候被称为断言，它们是特殊类型的 ***非捕获组***（用于匹配模式，但不包括在匹配列表中）。当我们在一种特定模式之前或者之后有这种模式时，会优先使用断言。\n例如我们想获取输入字符串 `$4.44 and $10.88` 中带有前缀 `$` 的所有数字。我们可以使用这个正则表达式 `(?\u003c=\\$)[0-9\\.]*`，表示：获取包含 `.` 字符且前缀为 `$` 的所有数字。\n以下是正则表达式中使用的断言：\n\n|符号|描述|\n|:----:|----|\n|?=|正向先行断言|\n|?!|负向先行断言|\n|?\u003c=|正向后行断言|\n|?\u003c!|负向后行断言|\n\n### 4.1 正向先行断言\n\n正向先行断言认为第一部分的表达式的后面必须是先行断言表达式。返回的匹配结果仅包含与第一部分表达式匹配的文本。\n要在一个括号内定义一个正向先行断言，在括号中问号和等号是这样使用的 `(?=...)`。先行断言表达式写在括号中的等号后面。\n例如正则表达式 `(T|t)he(?=\\sfat)`，表示：匹配大写字母 `T` 或小写字母 `t`，后面跟字母 `h`，后跟字母 `e`。\n在括号中，我们定义了正向先行断言，它会引导正则表达式引擎匹配后面跟着 `fat` 的 `The` 或 `the`。\n\n\u003cpre\u003e\n\"(T|t)he(?=\\sfat)\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e fat cat sat on the mat.\n\u003c/pre\u003e\n\n### 4.2 负向先行断言\n\n当我们需要指定第一部分表达式的后面不跟随某一内容时，使用负向先行断言。负向先行断言的定义跟我们定义的正向先行断言一样，\n唯一的区别在于我们使用否定符号 `!` 而不是等号 `=`，例如 `(?!...)`。\n我们来看看下面的正则表达式 `(T|t)he(?!\\sfat)`，表示：从输入字符串中获取全部 `The` 或者 `the` 且不匹配 `fat` 前面加上一个空格字符。\n\n\u003cpre\u003e\n\"(T|t)he(?!\\sfat)\" =\u003e The fat cat sat on \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e mat.\n\u003c/pre\u003e\n\n### 4.3 正向后行断言\n\n正向后行断言用于获取跟随在特定模式之后的所有匹配内容。正向后行断言表示为 `(?\u003c=...)`。例如正则表达式 `(?\u003c=(T|t)he\\s)(fat|mat)`，表示：从输入字符串中获取在单词 `The` 或 `the` 之后的所有 `fat` 和 `mat` 单词。\n\n\u003cpre\u003e\n\"(?\u003c=(T|t)he\\s)(fat|mat)\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e cat sat on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n### 4.4 负向后行断言\n\n负向后行断言是用于获取不跟随在特定模式之后的所有匹配的内容。负向后行断言表示为 `(?\u003c!...)`。例如正则表达式 `(?\u003c!(T|t)he\\s)(cat)`，表示：在输入字符中获取所有不在 `The` 或 `the` 之后的所有单词 `cat`。\n\n\u003cpre\u003e\n\"(?\u0026lt;!(T|t)he\\s)(cat)\" =\u003e The cat sat on \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n## 5. 标记\n\n标记也称为修饰符，因为它会修改正则表达式的输出。这些标志可以以任意顺序或组合使用，并且是正则表达式的一部分。\n\n|标记|描述|\n|:----:|----|\n|i|不区分大小写：将匹配设置为不区分大小写。|\n|g|全局搜索：搜索整个输入字符串中的所有匹配。|\n|m|多行匹配：会匹配输入字符串每一行。|\n\n### 5.1 不区分大小写\n\n`i` 修饰符用于执行不区分大小写匹配。例如正则表达式 `/The/gi`，表示：大写字母 `T`，后跟小写字母 `h`，后跟字母 `e`。\n但是在正则匹配结束时 `i` 标记会告诉正则表达式引擎忽略这种情况。正如你所看到的，我们还使用了 `g` 标记，因为我们要在整个输入字符串中搜索匹配。\n\n\u003cpre\u003e\n\"The\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e fat cat sat on the mat.\n\u003c/pre\u003e\n\n\u003cpre\u003e\n\"/The/gi\" =\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003eThe\u003c/strong\u003e\u003c/a\u003e fat cat sat on \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ethe\u003c/strong\u003e\u003c/a\u003e mat.\n\u003c/pre\u003e\n\n### 5.2 全局搜索\n\n`g` 修饰符用于执行全局匹配（会查找所有匹配，不会在查找到第一个匹配时就停止）。\n例如正则表达式 `/.(at)/g`，表示：除换行符之外的任意字符，后跟小写字母 `a`，后跟小写字母 `t`。\n因为我们在正则表达式的末尾使用了 `g` 标记，它会从整个输入字符串中找到每个匹配项。\n\n\u003cpre\u003e\n\".(at)\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e cat sat on the mat.\n\u003c/pre\u003e\n\n\u003cpre\u003e\n\"/.(at)/g\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003ecat\u003c/strong\u003e\u003c/a\u003e \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003esat\u003c/strong\u003e\u003c/a\u003e on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat\u003c/strong\u003e\u003c/a\u003e.\n\u003c/pre\u003e\n\n### 5.3 多行匹配\n\n`m` 修饰符被用来执行多行的匹配。正如我们前面讨论过的 `(^, $)`，使用定位符来检查匹配字符是输入字符串开始或者结束。但是我们希望每一行都使用定位符，所以我们就使用 `m` 修饰符。\n例如正则表达式 `/at(.)?$/gm`，表示：小写字母 `a`，后跟小写字母 `t`，匹配除了换行符以外任意字符零次或一次。而且因为 `m` 标记，现在正则表达式引擎匹配字符串中每一行的末尾。\n\n\u003cpre\u003e\n\"/.at(.)?$/\" =\u003e The fat\n                cat sat\n                on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n\u003cpre\u003e\n\"/.at(.)?$/gm\" =\u003e The \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003efat\u003c/strong\u003e\u003c/a\u003e\n                  cat \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003esat\u003c/strong\u003e\u003c/a\u003e\n                  on the \u003ca href=\"#learn-regex\"\u003e\u003cstrong\u003emat.\u003c/strong\u003e\u003c/a\u003e\n\u003c/pre\u003e\n\n## 常用正则表达式\n\n* **正整数**：`^\\d+$`\n* **负整数**：`^-\\d+$`\n* **电话号码**：`^+?[\\d\\s]{3,}$`\n* **电话代码**：`^+?[\\d\\s]+(?[\\d\\s]{10,}$`\n* **整数**：`^-?\\d+$`\n* **用户名**：`^[\\w\\d_.]{4,16}$`\n* **字母数字字符**：`^[a-zA-Z0-9]*$`\n* **带空格的字母数字字符**：`^[a-zA-Z0-9 ]*$`\n* **密码**：`^(?=^.{6,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$`\n* **电子邮件**：`^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4})*$`\n* **IPv4 地址**：`^((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))*$`\n* **小写字母**：`^([a-z])*$`\n* **大写字母**：`^([A-Z])*$`\n* **网址**：`^(((http|https|ftp):\\/\\/)?([[a-zA-Z0-9]\\-\\.])+(\\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\\/+=%\u0026_\\.~?\\-]*))*$`\n* **VISA 信用卡号码**：`^(4[0-9]{12}(?:[0-9]{3})?)*$`\n* **日期（MM/DD/YYYY）**：`^(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}$`\n* **日期（YYYY/MM/DD）**：`^(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])$`\n* **万事达信用卡号码**：`^(5[1-5][0-9]{14})*$`\n\n## Contribution\n\n* Report issues\n* Open pull request with improvements\n* Spread the word \n\n## License\n\nMIT © [Zeeshan Ahmed](mailto:ziishaned@gmail.com)\n\n---\n\n**[⬆ top](#什么是正则表达式-)**\n","funding_links":[],"categories":["Others"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdoco%2Flearn-regex-zh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdoco%2Flearn-regex-zh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdoco%2Flearn-regex-zh/lists"}