Regex Cheat Sheet
交互式正则表达式参考,支持实时模式测试和单击复制示例。
在线测试器
Anchors
| 模式 | 说明 | |
|---|---|---|
| ^ | Start of string / line | |
| $ | End of string / line | |
| \b | Word boundary | |
| \B | Non-word boundary | |
| \A | Start of string only | |
| \Z | End of string only |
Quantifiers
| 模式 | 说明 | |
|---|---|---|
| * | 0 or more times | |
| + | 1 or more times | |
| ? | 0 or 1 time (optional) | |
| {n} | Exactly n times | |
| {n,} | n or more times | |
| {n,m} | Between n and m times | |
| *? | Lazy 0 or more | |
| +? | Lazy 1 or more |
Character Classes
| 模式 | 说明 | |
|---|---|---|
| . | Any character except newline | |
| \d | Digit [0-9] | |
| \D | Non-digit | |
| \w | Word char [a-zA-Z0-9_] | |
| \W | Non-word character | |
| \s | Whitespace | |
| \S | Non-whitespace | |
| [abc] | Character set — a, b, or c | |
| [^abc] | Negated set — not a, b, or c | |
| [a-z] | Character range |
Groups & Alternation
| 模式 | 说明 | |
|---|---|---|
| (abc) | Capturing group | |
| (?:abc) | Non-capturing group | |
| (?<name>abc) | Named capturing group | |
| (?=abc) | Positive lookahead | |
| (?!abc) | Negative lookahead | |
| (?<=abc) | Positive lookbehind | |
| (?<!abc) | Negative lookbehind | |
| a|b | Alternation — a or b |
Flags
| 模式 | 说明 | |
|---|---|---|
| g | Global — find all matches | |
| i | Case-insensitive | |
| m | Multiline — ^ and $ match line boundaries | |
| s | Dotall — . matches newline too | |
| u | Unicode mode | |
| y | Sticky — match at exact position |
Common Patterns
| 模式 | 说明 | |
|---|---|---|
| ^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$ | Email address | |
| https?:\/\/[\w\-._~:/?#[\]@!$&'()*+,;=%]+ | URL (http/https) | |
| ^\+?[1-9]\d{1,14}$ | Phone number (E.164) | |
| ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ | Date (YYYY-MM-DD) | |
| ^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$ | IPv4 address | |
| ^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$ | UUID v4 | |
| ^#(?:[0-9a-fA-F]{3}){1,2}$ | Hex color code | |
| ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$ | Strong password (min 8 chars, upper+lower+digit) |
点击一个模式将其加载到测试器中
关于此工具
正则表达式是一种强大的模式匹配语言,广泛应用于编程、数据处理、文本分析和众多应用程序中。由于正则表达式的语法简洁但特殊字符众多,学习可能显得困难,但掌握基础概念——锚点、量词、字符类、分组和前后查看——就能优雅地解决复杂的文本问题。本正则表达式速查表将最必要的模式和概念汇聚在一个交互式参考工具中。
按类别浏览精选的模式和概念:锚点(^、$、\b)、量词(*、+、?、{n,m})、字符类([...]、\d、\s)、分组和捕获(...、?:...)、前后查看(正向和反向)。实时模式测试器让您输入自己的测试字符串和正则表达式,实时看到匹配结果高亮显示。点击一次复制任何模式,直接粘贴到代码或正则表达式调试工具中。
在需要快速复习语法时将此工具加入书签。无论是验证电子邮件地址、解析日志文件、从HTML提取数据、清理杂乱字符串还是搜索代码,正则表达式都是不可或缺的。本工具展示的模式与大多数语言(JavaScript、Python、Java、Go等)兼容,但某些高级功能在不同实现中可能略有差异。
常见问题
代码实现
import re
# Common regex patterns
email_pattern = r'^[w.-]+@[w.-]+.[a-zA-Z]{2,}$'
url_pattern = r'https?://[w-._~:/?#[]@!$&'()*+,;=%]+'
ipv4_pattern = r'^(?:(?:25[0-5]|2[0-4]d|[01]?dd?).){3}(?:25[0-5]|2[0-4]d|[01]?dd?)$'
# Test email
email = "user@example.com"
if re.match(email_pattern, email):
print(f"{email} is valid")
# Find all matches
text = "Contact us at info@example.com or support@test.org"
emails = re.findall(r'[w.-]+@[w.-]+.[a-zA-Z]{2,}', text)
print("Found emails:", emails)
# Named groups
date_text = "Today is 2024-03-15"
match = re.search(r'(?P<year>d{4})-(?P<month>d{2})-(?P<day>d{2})', date_text)
if match:
print(f"Year: {match.group('year')}, Month: {match.group('month')}")
# Substitution
result = re.sub(r's+', ' ', "hello world foo").strip()
print(result) # "hello world foo"Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.