Regular Expressions: A Practical Introduction

Regular Expressions: A Practical Introduction

Regular expressions (regex) are one of the most powerful tools in programming—and one of the most intimidating. This guide demystifies them with practical examples you'll actually use.

What Are Regular Expressions?

A regular expression is a pattern that describes a set of strings. Instead of searching for exact text, you search for text that matches a pattern.

Think of it like wildcards on steroids. Where a wildcard * matches anything, regex lets you specify exactly what "anything" means.

Basic Building Blocks

Literal Characters

Most characters match themselves. The regex cat matches the text "cat".

Special Characters

These have special meanings and need escaping with if you want to match them literally:

. ^ $ * + ? { } [ ] | ( )

The Dot (.)

Matches any single character except newline.

  • c.t matches "cat", "cot", "cut", "c9t"

Character Classes

Square brackets define a set of characters:

  • [abc] matches "a", "b", or "c"
  • [a-z] matches any lowercase letter
  • [0-9] matches any digit
  • [^abc] matches anything except "a", "b", or "c"

Shorthand Classes

  • d - digit [0-9]
  • w - word character [a-zA-Z0-9_]
  • s - whitespace (space, tab, newline)
  • D, W, S - negations of above

Test patterns with our regex tester.

Quantifiers

Specify how many times a pattern should match:

  • * - zero or more
  • + - one or more
  • ? - zero or one (optional)
  • {3} - exactly 3
  • {2,5} - between 2 and 5
  • {3,} - 3 or more

Examples:

  • a* matches "", "a", "aa", "aaa"...
  • a+ matches "a", "aa", "aaa"... (but not "")
  • colou?r matches "color" and "colour"
  • d{3}-d{4} matches "555-1234"

Anchors

Match positions, not characters:

  • ^ - start of string/line
  • $ - end of string/line
  •  - word boundary

Examples:

  • ^Hello matches "Hello" only at the start
  • end$ matches "end" only at the end
  • cat matches "cat" but not "category"

Groups and Alternation

Parentheses for Grouping

(ab)+ matches "ab", "abab", "ababab"

Alternation (OR)

cat|dog matches "cat" or "dog"

(Mon|Tues|Wednes)day matches three days

Capture Groups

Parentheses capture matched text for reuse:

Practical Examples

Email Validation

^[w.-]+@[w.-]+.w{2,}$

Basic email pattern (production systems need more comprehensive patterns)

Phone Numbers

(?(d{3}))?[-.s]?(d{3})[-.s]?(d{4})

Matches (555) 123-4567, 555-123-4567, 555.123.4567

URL Matching

https?://[w.-]+(/[w.-]*)*/?

Matches HTTP and HTTPS URLs

Date Formats

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

Matches YYYY-MM-DD format

Password Strength

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

Requires lowercase, uppercase, digit, 8+ characters

Greedy vs. Lazy Matching

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

  • ".+" on "a" and "b" matches the whole thing
  • ".+?" (lazy) matches just "a" and then "b"

Add ? after a quantifier to make it lazy.

Common Mistakes

  1. Forgetting to escape: . matches everything; use . for literal period
  2. Over-complication: Start simple, add complexity as needed
  3. No anchors: d+ matches "123" in "abc123def"—use anchors if you need full match
  4. Catastrophic backtracking: Some patterns can cause exponential runtime

Regex Tools

Regular expressions take practice. Use our regex tester to experiment with patterns until they feel natural.

Try Regex Tester Now