Skip to main content

Regex Cheatsheet

Regular expressions are the Swiss Army knife of text processing, used across tools like grep, sed, awk, vim and languages like Python, JavaScript, and Java. This cheatsheet organizes regex syntax from basic to advanced — literal matching, quantifiers, groups & captures, lookaround assertions, flags, replacement references, and flavor differences — each with practical examples.

Updated: 2026-07-16·32 commands

使用场景速查

你要做什么用哪个模式
匹配数字\d+[0-9]+
匹配邮箱[\w.]+@\w+\.\w+
匹配 URLhttps?://[\w./]+
提取括号内容\(([^)]+)\)
替换前后文本(?<=before)text(?=after)
忽略大小写/pattern/i 标志位
匹配行首行尾^pattern$

字面量与字符类

``regex # 字面量匹配 hello # 直接匹配 "hello" 字符串 abc123 # 匹配字母数字组合 a\.b # 转义点号(匹配 "a.b" 而不是任意字符) a\\b # 匹配 "a\\b"(反斜杠需要双重转义)

# 字符类 [abc] # 匹配 a、b、c 中任意一个 [a-z] # 小写字母范围 [0-9] # 数字范围 [a-zA-Z0-9_] # 字母数字下划线 [^abc] # 排除 a、b、c(取反) [\s\S] # 匹配任意字符(包括换行)

# 预定义字符类 \d # 数字 [0-9] \D # 非数字 \w # 单词字符 [a-zA-Z0-9_] \W # 非单词字符 \s # 空白字符 [ \t\n\r\f\v] \S # 非空白字符 . # 匹配任意单个字符(默认不包含换行)

# 特殊字符转义 \. \* \\ \( \) \[ \] \+ \? \^ \$ # 元字符转义后匹配字面量 `

量词

`regex # 基础量词 a* # 0 次或多次 a+ # 1 次或多次 a? # 0 次或 1 次(可选) a{3} # 精确匹配 3 次 a{2,5} # 2 到 5 次 a{2,} # 至少 2 次

# 贪婪 vs 非贪婪 a.*b # 贪婪:匹配 a 到最后一个 b a.*?b # 非贪婪:匹配 a 到第一个 b a.+?b # 非贪婪:至少一个字符 a{2,5}? # 非贪婪:尽可能少匹配

# 实用示例 \d{3,4} # 3 或 4 位数字 \w+ # 一个或多个单词字符 https?:// # 匹配 http:// 或 https:// \.\w{2,4}$ # 匹配文件扩展名(2-4个字符) `

分组与捕获

`regex # 捕获组 (abc) # 捕获组 #1 (a)(b)(c) # 三个捕获组 #1, #2, #3 ((a)(b)) # 嵌套捕获组 #1=ab, #2=a, #3=b

# 反向引用(同一模式内) (abc)\1 # 匹配 "abcabc"(\1 引用组 #1) <(\w+)>.*<\/\1> # 匹配 HTML 标签对:... (["'])\w+\1 # 匹配引号内的单词:"hello" 或 'hello'

# 非捕获组 (?:abc) # 分组但不捕获 (?:https?|ftp):// # 匹配协议前缀(http/ftp)

# 命名捕获组(PCRE/Python) (?Ppattern) # Python 命名捕获 (?pattern) # PCRE/JavaScript 命名捕获 \k # 引用命名捕获组

# 原子分组(禁止回溯) (?>pattern) # 原子分组,匹配后不回溯 `

环视断言

`regex # 前瞻断言 (?=pattern) # 肯定前瞻:后面跟着 pattern (?!pattern) # 否定前瞻:后面不跟 pattern

# 后顾断言 (?<=pattern) # 肯定后顾:前面是 pattern (?

# 实用示例 (?<=\$)\d+ # 美元符号后的数字($100 -> 100) \w+(?=\s+is) # "is" 前面的单词 (?!admin)\w+ # 匹配不是 "admin" 的单词 (?

# 复杂环视组合 (?<=@)\w+(?=\.) # 邮箱中 @ 和 . 之间的部分 (?<=).*?(?=) # 提取标签之间的内容 `

标志位

`regex # 常用标志 /i # 忽略大小写 /g # 全局匹配(查找所有匹配) /m # 多行模式(^$ 匹配行首尾) /s # 单行/DOTALL 模式(. 匹配换行) /x # 宽松模式(忽略空格和注释) /u # Unicode 支持 /y # 粘性匹配(从上次结束位置继续)

# 内联标志(模式内) (?i)abc # 从 (?i) 开始忽略大小写 (?i-m)abc # 启用忽略大小写,禁用多行 (?-i) # 关闭忽略大小写

# 组合示例 /^hello/i # 行首的 hello(忽略大小写) /^hello/im # 每行行首的 hello /hello.world/s # hello.world 跨行匹配 `

替换引用

`regex # 替换语法 $1 $2 ... # 引用捕获组(多数语言/工具) \1 \2 ... # 引用捕获组(sed、vim) $& 或 \0 # 整个匹配的文本 $ 或 \ # 匹配前的文本 $' 或 \' # 匹配后的文本

# 实用替换 s/(\w+)@(\w+)/$1 at $2/g # "user@host" -> "user at host" s/(\d{4})-(\d{2})-(\d{2})/$2\/$3\/$1/ # 日期格式转换 2026-07-16 -> 07/16/2026 s/\b(\w)/\U$1/g # 每个单词首字母大写 s/\s+/ /g # 多个空格合并为一个

# 条件替换 s/foo/bar/ # 只替换第一个匹配 s/foo/bar/g # 替换所有匹配 s/(?<=,)\s+/ /g # 逗号后的空格缩为一个

# 大小写转换(部分工具支持) \L...\E # 转小写(sed) \U...\E # 转大写(sed) \l \u # 下一个字符小写/大写 `

风格差异

`bash # BRE(基本正则 - sed/grep 默认) \(abc\) # 需要转义才能捕获 \{3\} # 需要转义才能用量词 a\+ # 需要转义才能匹配 1 次以上 a\|b # 需要转义才能用或

# ERE(扩展正则 - grep -E / sed -E) (abc) # 直接使用捕获组 {3} # 直接使用量词 a+ # 直接使用 + a|b # 直接使用或

# PCRE(Perl 兼容 - Python/JS/Java/PHP) \d+ # 预定义字符类 (?=) (?!) (?<=) (?

# grep 风格 grep 'pattern' file # 基本正则(BRE) grep -E 'pattern' file # 扩展正则(ERE) grep -P 'pattern' file # PCRE(如果支持) grep -o 'pattern' file # 只输出匹配部分

# sed 风格 sed 's/pattern/replacement/' # BRE sed -E 's/pattern/replacement/' # ERE sed -n 's/pattern/replacement/p' # 打印替换结果

# awk 风格 awk '/pattern/ {print}' # ERE 类似 awk '{ gsub(/pattern/, "repl"); print }' # 替换 ``

Literals(8)

CommandLevel
.
Match any single character (excludes newline by default)
Basic
[abc]
Character class — match any one character in the set
Basic
[^abc]
Negated character class — match any character NOT in the set
Basic
\d / \w / \s
Predefined classes — digit, word char, whitespace
Basic
\D / \W / \S
Negated predefined classes
Basic
\. \* \\ \( \)
Escape metacharacters — match them literally
Basic
^ $
Start-of-line and end-of-line anchors
Basic
\b / \B
Word boundary and non-word boundary assertions
Intermediate

Quantifiers(4)

CommandLevel
* / + / ?
Basic quantifiers — zero-or-more, one-or-more, zero-or-one
Basic
{n,m}
Range quantifier — specify exact repetition range
Intermediate
*? / +? / ??
Lazy quantifiers — match as little as possible
Intermediate
.* / .+
Repeat any char — match arbitrary string length
Basic

Groups(5)

CommandLevel
(pattern)
Capturing group — match and save substring for backreference
Basic
(?:pattern)
Non-capturing group — group without saving match
Intermediate
\1 \2 $1 $2
Backreference — reference previously captured content
Intermediate
(?P<name>pattern)
Named capturing group — reference by name
Expert
(?>pattern)
Atomic group — once matched, never backtrack
Expert

Lookaround(4)

CommandLevel
(?=pattern)
Positive lookahead — assert followed by pattern
Intermediate
(?!pattern)
Negative lookahead — assert NOT followed by pattern
Intermediate
(?<=pattern)
Positive lookbehind — assert preceded by pattern
Expert
(?<!pattern)
Negative lookbehind — assert NOT preceded by pattern
Expert

Flags(5)

CommandLevel
/i
Case-insensitive flag
Basic
/g
Global match flag (find all matches)
Basic
/m
Multiline flag (^$ match line starts/ends)
Intermediate
/s
Single-line/DOTALL flag (. matches newlines)
Intermediate
(?i) inline flag
Inline flags — enable/disable flags within pattern
Expert

Replacement(3)

CommandLevel
$1 / \1 backreference
Backreference in replacement — reference captured groups
Intermediate
$& $MATCH $PREMATCH
Special replacement vars -- full match, before/after text
Intermediate
\U / \L / \u / \l
Case conversion in replacement (sed and some tools)
Expert

Flavors(3)

CommandLevel
BRE -- \( \) \{ \}
Basic Regular Expressions (BRE) — metacharacters need escaping
Intermediate
ERE -- ( ) { } + |
Extended Regular Expressions (ERE) — metacharacters work directly
Intermediate
PCRE -- \d \s \b (?=)
Perl-Compatible Regex (PCRE) — richest feature set
Intermediate

FAQ

This cheatsheet is compiled from official tool documentation. Last updated: 2026-07-16.