← Back to Blog

Regular Expressions for Beginners: Patterns, Flags, and Real Examples

Stop guessing at patterns — understand how regex actually works

What Is a Regular Expression?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. You use regex to find text that matches a pattern, validate input formats, extract parts of a string, or replace matched text. Regex is supported in virtually every programming language and is one of the most powerful tools in a developer's toolkit.

Basic Syntax

Literal characters

The simplest regex is just a literal string. The pattern hello matches the exact text "hello" anywhere in a string.

The dot (.)

A dot matches any single character except a newline. h.llo matches "hello", "hallo", "hxllo", etc.

Character classes []

[aeiou] matches any single vowel. [0-9] matches any digit. [a-zA-Z] matches any letter.

Quantifiers

Anchors

Shorthand classes

Flags

Flags modify how the entire pattern is applied:

Real-World Examples

Email address

\b[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}\b

Phone number (US format)

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

URL

https?:\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:/~+#]*)

Hex color code

#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})\b

Capture Groups and Backreferences

Parentheses () create a capture group — a portion of the match you can extract or reuse. Given the pattern (\d{4})-(\d{2})-(\d{2}) applied to a date like "2026-07-12", group 1 captures "2026", group 2 captures "07", and group 3 captures "12". This is how you pull structured pieces out of an otherwise unstructured string, and it's the basis of "find and replace with capture groups" — for example, reformatting YYYY-MM-DD to MM/DD/YYYY using a replacement pattern like $2/$3/$1.

Non-capturing groups, written (?:...), group parts of a pattern for the purposes of applying a quantifier without creating a numbered capture — useful when you need grouping but don't need to extract that piece.

Advertisement

Greedy vs Lazy Matching

By default, quantifiers are greedy — they match as much text as possible. Given the input <b>bold</b> and <b>more</b>, the pattern <b>.*</b> will greedily match from the very first <b> all the way to the very last </b>, swallowing both tags in one match. Adding a ? after the quantifier makes it lazy instead: <b>.*?</b> stops at the first </b> it finds, matching each tag pair separately. This distinction trips up beginners constantly and is worth testing explicitly whenever a match looks "too long."

Common Regex Mistakes

Regex Across Languages

The core syntax covered here (character classes, quantifiers, anchors, groups) is shared across JavaScript, Python, Java, and most other languages, since they all descend from the same Perl-compatible regular expression (PCRE) tradition. The differences tend to be small but important: JavaScript requires flags after the closing slash (/pattern/gi), Python uses a separate re module with flags passed as arguments, and named capture group syntax varies slightly between languages.

Quick FAQ

Why does my pattern match more text than I expected? Almost always a greedy quantifier — try making it lazy with a ?, or use a more specific character class instead of ..

How do I match a literal special character like a period or parenthesis? Escape it with a backslash: \., \(, \).

Should I use regex to validate email addresses? A simple pattern catches obvious typos, but a fully RFC-compliant email regex is extremely long and still won't confirm the address actually exists — pair regex validation with a confirmation email for anything that matters.

Test Your Regex Instantly

Use the DataBench Regex Tester to write and test patterns with live match highlighting, capture group display, and flag support. Runs entirely client-side.

Advertisement