惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
B
Blog
博客园_首页
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
M
MIT News - Artificial intelligence
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
量子位
V
V2EX
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
I
InfoQ
博客园 - 【当耐特】

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers Python Reverse a List New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode
TIL: i Hate Regex - The Ultimate Regex Cheat Sheet
2021-03-09 · via Stonecharioteer on Tech

i Hate Regex - Making Regex Approachable

i Hate Regex - The Regex Cheat Sheet

A beautifully designed, interactive resource for learning and using regular expressions:

Why This Resource Exists:

  • Universal Struggle: Most developers find regex intimidating
  • Scattered Information: Regex resources are often fragmented or overly technical
  • Learning Curve: Traditional regex documentation is hard to parse
  • Practical Focus: Need real-world patterns, not academic theory

What Makes It Special:

Interactive Design:

  • Visual Explanations: Each regex pattern is broken down visually
  • Live Testing: Test patterns against sample text immediately
  • Color Coding: Different parts of regex highlighted for clarity
  • Copy-Paste Ready: All patterns ready for immediate use

Common Patterns Library:

  • Email Validation: Various levels of email regex complexity
  • Phone Numbers: International and country-specific formats
  • URLs: Web address matching with protocol handling
  • Credit Cards: Payment card number validation
  • Dates: Multiple date format patterns
  • Passwords: Security requirement patterns

Email Validation:

^[^\s@]+@[^\s@]+\.[^\s@]+$
  • Basic Pattern: Covers most common email formats
  • Explanation: Non-whitespace + @ + domain + . + extension
  • Use Case: Form validation, data cleaning

Phone Number (US):

^(\+1\s?)?(\([0-9]{3}\)|[0-9]{3})[\s\-]?[0-9]{3}[\s\-]?[0-9]{4}$
  • Flexible Format: Handles various US phone number styles
  • Options: With/without country code, parentheses, dashes
  • Examples: (555) 123-4567, 555-123-4567, +1 555 123 4567

URL Matching:

https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
  • Protocol Support: HTTP and HTTPS
  • Subdomain Handling: Optional www prefix
  • Query Parameters: Full URL with parameters supported

Learning Approach:

Progressive Complexity:

  1. Basic Patterns: Start with simple character matching
  2. Common Use Cases: Real-world validation patterns
  3. Advanced Features: Lookaheads, groups, modifiers
  4. Best Practices: Performance and maintainability tips

Visual Learning:

  • Breakdown Diagrams: Each part of regex explained
  • Match Highlighting: Shows what gets matched
  • Group Visualization: Capture groups clearly marked
  • Error Examples: Shows what doesn’t match and why

Practical Applications:

Form Validation:

1
2
3
// Email validation in JavaScript
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const isValidEmail = emailRegex.test(userInput);

Data Cleaning:

1
2
3
4
5
import re

# Extract phone numbers from text
phone_pattern = r'\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}'
phones = re.findall(phone_pattern, text)

Log File Analysis:

1
2
# Find IP addresses in log files
grep -E '\b([0-9]{1,3}\.){3}[0-9]{1,3}\b' access.log

Beyond the Cheat Sheet:

Testing Tools:

  • Regex101: Advanced regex testing and explanation
  • RegExr: Interactive regex builder and tester
  • Built-in Tools: Most code editors have regex find/replace

Performance Considerations:

  • Catastrophic Backtracking: Avoid nested quantifiers
  • Compilation: Compile frequently used patterns
  • Alternatives: Sometimes string methods are faster

Best Practices Learned:

  1. Start Simple: Build complex patterns incrementally
  2. Test Thoroughly: Use diverse test cases
  3. Document Intent: Comment complex patterns
  4. Consider Alternatives: Regex isn’t always the best solution
  5. Performance Matters: Profile regex-heavy code

Common Pitfalls to Avoid:

  • Overcomplicating: Don’t try to handle every edge case
  • Greedy Matching: Understand when to use lazy quantifiers
  • Escaping Issues: Remember to escape special characters
  • Language Differences: Regex flavors vary between languages

i Hate Regex transforms the intimidating world of regular expressions into an accessible, visual learning experience that focuses on practical, everyday use cases.