Programming

What is a Regular Expression (Regex)?

A regular expression is a pattern used to match, search, and manipulate text based on rules rather than exact strings.

A regular expression (regex) is a sequence of characters that defines a search pattern for text. Instead of matching exact strings, you describe rules — like "one or more digits" or "an email-shaped string" — and the regex engine finds matches.

How It Works:

  1. You write a pattern using special syntax
  2. A regex engine scans text looking for matches
  3. It returns matches, positions, or lets you replace them

Common Building Blocks:

  • . — any character
  • * — zero or more of the previous
  • + — one or more
  • \d — a digit, \w — a word character
  • [abc] — any of a, b, or c
  • ^ and $ — start and end of a line
  • ( ) — capture groups

Where It's Used:

  • Validation: Emails, phone numbers, passwords
  • Search & replace: In editors and scripts
  • Parsing: Extracting data from text
  • Log analysis: Finding patterns in output

FAQ

Why do regular expressions look so cryptic?

They pack a lot of meaning into few characters. Once you learn the core symbols, patterns become readable — but complex ones are still worth commenting for clarity.

Are regular expressions the right tool for parsing HTML?

Usually not. HTML is nested and irregular, which regex handles poorly. Use a proper parser for structured formats like HTML, XML, or JSON.

Promote your content

Reach over 400,000 developers and grow your brand.

Join our developer community

Hang out with over 4,500 developers and share your knowledge.