Developer

Regex for Beginners — How Regular Expressions Work (With Real Examples)

Regular expressions look intimidating but follow logical rules. Learn the core syntax, common patterns for email and phone validation, and how to test regex patterns instantly.

·9 min read·TOOLBeans Team
🔍

Free Tool

Regex Tester

No account · No install · Runs in browser

Open Tool →

Why Regular Expressions Seem Hard (And Why They Are Not)

Regular expressions — regex — look like line noise. ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ is not exactly welcoming. But once you understand that each piece has a specific meaning that follows consistent rules, regex becomes a powerful readable tool rather than an intimidating string of symbols.

Regex is supported in virtually every programming language and many text editors. Learning the basics pays dividends across your entire career because the same patterns work everywhere.

The Core Building Blocks

Literal characters — Most characters match themselves. The pattern cat matches the string "cat" exactly. Simple.

The dot. matches any single character except a newline. The pattern c.t matches "cat", "cot", "cut", "c3t", anything with any character between 'c' and 't'.

Character classes — Square brackets define a set of characters to match. [aeiou] matches any vowel. [a-z] matches any lowercase letter. [0-9] matches any digit. [^aeiou] matches any character that is not a vowel (the caret inside brackets means NOT).

Quantifiers — Control how many times something matches.

  • * — zero or more
  • + — one or more
  • ? — zero or one (optional)
  • {3} — exactly three times
  • {2,5} — between two and five times

Anchors^ matches the start of a string. $ matches the end. Without anchors, a pattern can match anywhere in a string. With them, you can ensure the entire string matches your pattern.

Common Patterns You Will Actually Use

Email validation (simplified):

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

Reads as: Start of string, one or more email-valid characters, an @ sign, one or more domain characters, a dot, two or more letters for the TLD, end of string.

Phone number (US):

^+?1?[-.s]?(?[0-9]{3})?[-.s]?[0-9]{3}[-.s]?[0-9]{4}$

Handles 555-1234, (555) 123-4567, +1 555.123.4567, and other common US formats.

URL detection:

https?://[^s]+

The s? makes the 's' in 'https' optional (matches both http and https). Then matches any non-whitespace characters after the domain.

Only digits:

^d+$

d is shorthand for [0-9]. Combined with anchors, this matches strings containing only digits.

The Flags That Change Behavior

Regex flags modify how the pattern is interpreted.

i (case-insensitive) — /cat/i matches "cat", "Cat", "CAT", "cAt".

g (global) — Find all matches, not just the first one.

m (multiline) — Makes ^ and $ match the start and end of each line rather than the whole string.

Testing Regex Without Frustration

The best way to learn regex is to write a pattern and immediately see what it matches and what it misses. Our Regex Tester lets you write the pattern, paste test text, and highlights all matches in real time.

When a pattern does not match what you expect, you can change it character by character and see the effect immediately — much faster than running code in a terminal and much more visual than reading documentation.

Related Topics

regex testerregular expressionsregex tutorialregex for beginnersregex email validationlearn regex
🔍

Try it yourself

Regex Tester

Everything in this article is available in the free tool. No account, no subscription, no install.

Open Regex Tester →