Regular expressions (regex) intimidate beginners with their cryptic syntax: `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$`. But regex is just pattern matching — learnable and incredibly powerful. Here's a beginner's guide.

What Is Regex?

A regex is a sequence of characters that defines a search pattern. It's used for:

  • Email validation
  • URL/phone number extraction
  • Data cleaning
  • Finding and replacing text
  • Password strength validation

Basic Syntax

Literal Characters

cat       # Matches "cat" exactly
hello     # Matches "hello"

Metacharacters (Special Meaning)

  • . — Any character (except newline)
  • * — Zero or more of preceding
  • + — One or more of preceding
  • ? — Zero or one of preceding
  • {n} — Exactly n of preceding
  • {n,m} — Between n and m of preceding
  • ^ — Start of line
  • $ — End of line
  • | — OR operator
  • [] — Character class
  • () — Grouping
  • \ — Escape special character

Character Classes

[abc]     # Match a, b, or c
[a-z]     # Match any lowercase letter
[A-Z]     # Match any uppercase letter
[0-9]     # Match any digit
[^abc]    # Match anything EXCEPT a, b, c

Shortcuts

\d       # Any digit (0-9)
\D       # Any non-digit
\w       # Word character (a-z, A-Z, 0-9, _)
\W       # Non-word character
\s       # Whitespace
\S       # Non-whitespace

Common Patterns

Email

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Phone Number (US)

^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$

URL

^https?:\/\/[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}.*$

Strong Password (8+ chars, uppercase, lowercase, digit)

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Hex Color

^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

Testing Your Regex

Use our Regex Tester to write and test patterns interactively:

  1. Paste or write your regex
  2. Enter test strings
  3. See matches in real-time
  4. Get detailed match info

Regex Flags

  • i — Case-insensitive matching
  • g — Global (find all, not just first)
  • m — Multiline (^ and $ match line breaks)

Common Mistakes

1. Greedy vs Lazy Matching

Greedy: `.*` matches as much as possible. Lazy: `.*?` matches as little as possible.

2. Not Escaping Special Characters

To match a literal `.` use `\.` not just `.`

3. Assuming Regex Validates Email/URL Perfectly

A perfect email regex is 6,000+ characters. Use built-in validators when possible.

4. Forgetting to Anchor

Without `^` and `$`, partial matches count. `cat` matches "catalog".

Practical Examples

Extract All URLs from Text

https?:\/\/[^\s]+ 

Replace Multiple Spaces with One

Find:    \s+
Replace: (space)

Validate Credit Card (basic)

^\d{13,19}$

Pro Tips

  • Start simple, build complexity incrementally
  • Use comments in complex regex (some engines support `(?#comment)`)
  • Test edge cases (empty strings, special chars, etc.)
  • Use word boundaries `\b` to match whole words only
  • When stuck, break into smaller pieces

Conclusion

Regex isn't magic — it's a learnable skill that pays dividends. Start with our cheat sheet, practice with simple patterns, and use the Regex Tester to build confidence. Within an hour, you'll understand 80% of regex use cases.