Regex Fundamentals

💤0
Lv 10 XP
← ⚙️ Scripting & Automation · Regular Expressions

Regex Fundamentals

Beginner ⭐ 50 XP ⏱ 18 min #regex#patterns

Match text patterns with the core regex syntax used across tools and languages.

📖Theory

A regular expression describes a text pattern. The same core syntax works in grep, Python, JavaScript, and most editors. The building blocks:

  • Literals match themselves; metacharacters (. * + ? [] () ^ $) are special
  • Character classes: \d digit, \w word char, \s whitespace, [a-z] range
  • Quantifiers: * (0+), + (1+), ? (0 or 1), {2,4} (range)
  • Anchors: ^ start of line, $ end of line, \b word boundary

Build patterns incrementally and test them on real samples — a tool like regex101 shows exactly what each part matches.

🌍Real-World Example
\d{3}-\d{4}            matches 555-1234
^ERROR                 lines starting with ERROR
\.log$                 strings ending in .log
[A-Za-z0-9._%+-]+@…    the local part of an email
\bcat\b                the word "cat", not "category"
grep -E '\bERROR\b' app.log        # whole word ERROR
grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}' log   # lines starting with a date
✍️Hands-On Exercise
  1. Write a pattern that matches a 5-digit US ZIP code.
  2. Match all lines that start with a timestamp like 2026-06-22.
  3. Match a filename ending in .log or .txt.
  4. Explain why \. is needed to match an IP address literally.
🧾Cheat Sheet
TokenMatches
.any character
\d \w \sdigit / word / whitespace
[abc] [^abc]set / negated set
* + ?0+, 1+, 0 or 1
{n,m}between n and m
^ $line start / end
\bword boundary
a|ba or b
💬Common Interview Questions
What does the dot . match, and how do you match a literal dot?

. matches any single character (except newline by default). Escape it as \. to match a literal period.

What's the difference between * and + ?

* matches zero or more of the preceding element; + matches one or more (so it requires at least one occurrence).

What does \b do?

It’s a word boundary anchor — \bcat\b matches the standalone word “cat” but not “category” or “scatter”.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type