Regex Tutorial: Learn Regular Expressions for Beginners

Published February 4, 2026 • 15 min read

Regular expressions (regex) look intimidating but are incredibly powerful for text processing. This tutorial teaches you regex from scratch with practical examples you can test immediately.

What is Regex?

Regex is a pattern-matching language for strings. Think of it as a supercharged "Find" function. Instead of searching for exact text, regex lets you search for patterns like:

Basic Syntax

Literal Characters

Most characters match themselves:

cat matches "cat" in "My cat is cute"

The Dot (.)

Matches any single character (except newline):

c.t matches "cat", "cot", "c9t", etc.

Character Classes

\d  matches any digit (0-9)
\w  matches any word character (a-z, A-Z, 0-9, _)
\s  matches any whitespace (space, tab, newline)
\D  matches any NON-digit
\W  matches any NON-word character
\S  matches any NON-whitespace

Quantifiers

Specify how many times a pattern should match:

*     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

Examples:

\d+        matches "123", "4567", "8"
\d{3}      matches exactly 3 digits
\d{2,4}    matches 2 to 4 digits
colou?r    matches "color" or "colour"

Anchors

^   matches start of string
$   matches end of string
\b  matches word boundary

Examples:

^Hello     matches "Hello" only at start
world$     matches "world" only at end
\bcat\b    matches "cat" as whole word (not "catch")

Groups and Alternation

Groups ()

(cat|dog)  matches "cat" OR "dog"
(ab)+      matches "ab", "abab", "ababab", etc.

Character Sets []

[aeiou]    matches any vowel
[0-9]      matches any digit (same as \d)
[a-z]      matches any lowercase letter
[^0-9]     matches any NON-digit

Common Patterns

Email Address

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

Phone Number (US)

\d{3}-\d{3}-\d{4}  or  \(\d{3}\) \d{3}-\d{4}

URL

https?://[^\s]+

Hex Color

#[0-9A-Fa-f]{6}

Date (YYYY-MM-DD)

\d{4}-\d{2}-\d{2}

Greedy vs Non-Greedy

By default, quantifiers are greedy (match as much as possible):

String: "The <b>bold</b> and <i>italic</i> text"
Pattern: <.*>
Greedy match: "<b>bold</b> and <i>italic</i>"  (everything!)

Pattern: <.*?>
Non-greedy match: "<b>"  (stops at first >)

Add ? after quantifiers to make them non-greedy: *?, +?, {n,}?

Flags/Modifiers

g  global (find all matches, not just first)
i  case-insensitive
m  multiline (^ and $ match line breaks)
s  dotall (. matches newlines too)

Escaping Special Characters

To match literal special characters, escape them with \:

\. matches a literal dot
\* matches a literal asterisk
\? matches a literal question mark
\\ matches a literal backslash

Practical Examples

Validate Strong Password

At least 8 chars, one uppercase, one lowercase, one digit:

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

Extract Hashtags

#\w+

Find IP Addresses

\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b

Match Credit Card Numbers

\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}

Common Mistakes

1. Forgetting to Escape

// Wrong:
example.com

// Right:
example\.com

2. Overly Greedy Matching

Use .*? instead of .* when extracting content between tags.

3. Not Using Anchors

// Matches "123" in "abc123def":
\d+

// Matches only if entire string is digits:
^\d+$

Testing Your Regex

Always test regex before deploying. Use our free regex tester to see matches highlighted in real-time.

Language Differences

Regex syntax is mostly consistent, but there are small differences:

Advanced Features

Lookahead

foo(?=bar)    matches "foo" only if followed by "bar"
foo(?!bar)    matches "foo" only if NOT followed by "bar"

Named Groups

(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})

When NOT to Use Regex

Practice Resources

Conclusion

Regex is a skill that pays dividends. Master the basics, practice with real examples, and you'll save hours on text processing tasks. Start simple and build complexity as needed.

Test Your Regex Now

Practice what you learned with our free regex tester. See matches highlighted in real-time.

Back to Blog

Share This