What Is Regex? A Beginner's Guide With Real Examples

By James Ephrim, founder of PrivateToolbox6 min read

I once spent an entire afternoon copying phone numbers out of a 400-row spreadsheet by hand because each one was buried in a messy notes column. A colleague watched me for a minute, typed a short line of odd-looking symbols into the search box, and highlighted every number in about two seconds. That line was a regular expression, and I've used them almost every week since.

Regex has a reputation for being cryptic, and the first time you see something like ^\d{3}-\d{4}$ it does look like a cat walked on the keyboard. But it's built from a small set of building blocks. In this guide I'll explain regex in plain English, share the ten patterns I find most useful, show you how to test patterns safely, and give you a cheat sheet to keep handy.

Regex in plain English

A regular expression (regex) is a search pattern. Instead of searching for an exact word, you describe the shape of what you're looking for: "three digits, a dash, then four digits" or "anything that looks like an email address".

Think of it as the difference between asking a librarian for "the book called Atlas" and asking for "every book with a one-word title starting with A". Regex lets you ask the second kind of question.

You'll find regex in code editors, spreadsheets (Google Sheets has REGEXMATCH), form validation on websites, search-and-replace in text editors, and nearly every programming language. Once you learn it, the same patterns work almost everywhere.

The core building blocks

  • Literal characters match themselves: cat matches "cat".
  • Character classes match one character from a set: [aeiou] matches any vowel; \d matches any digit.
  • Quantifiers say how many: + means one or more, * means zero or more, ? means optional, {3} means exactly three.
  • Anchors mark positions: ^ is the start of a line, $ is the end.
  • Groups use parentheses to treat several characters as one unit, and | means "or".

Put them together and \d{3}-\d{4} reads as: three digits, a dash, four digits.

The 10 most useful regex patterns

These are practical, readable patterns rather than the most exhaustive possible versions. Real-world data is messy, so test each one against your own examples.

  1. Email address: [\w.+-]+@[\w-]+\.[\w.-]+ — one or more name characters, an @, a domain, a dot, and an ending. It catches typical addresses without trying to cover every edge case in the official standard.
  2. US/Canada phone number: \(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4} — matches 555-123-4567, (555) 123 4567, and 555.123.4567.
  3. UK mobile number: (\+44\s?7\d{3}|07\d{3})\s?\d{3}\s?\d{3} — matches 07700 900123 and +44 7700 900123.
  4. Australian mobile number: (\+61\s?4|04)\d{2}\s?\d{3}\s?\d{3} — matches 0412 345 678 and +61 412 345 678.
  5. URL: https?://[^\s/$.?#].[^\s]* — anything starting with http:// or https:// up to the next space.
  6. Date in YYYY-MM-DD format: \d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) — a year, a valid month, and a valid day number.
  7. US ZIP code: \b\d{5}(-\d{4})?\b — five digits with an optional four-digit extension.
  8. Hex color code: #([0-9a-fA-F]{6}|[0-9a-fA-F]{3})\b — matches #1A2B3C and #fff.
  9. Repeated words: \b(\w+)\s+\1\b — finds accidental doubles like "the the". The \1 means "the same thing the first group matched".
  10. Extra whitespace: \s{2,} — two or more spaces in a row, perfect for cleaning pasted text. Replace matches with a single space.

For tidying text before matching — fixing case, collapsing spaces — our Case Converter & Text Cleaner handles the common jobs without writing a pattern at all.

How to test patterns safely

Never drop an untested regex into a spreadsheet macro, a production form, or a bulk find-and-replace. A tiny mistake can match far more (or less) than you meant. Test first, with real-looking examples.

  1. Open our Regex Tester.
  2. Paste sample text that includes both things you want to match and things you don't — for example, valid and invalid phone numbers.
  3. Type your pattern. Matches highlight live as you type, so you see instantly what each change does.
  4. Toggle flags as needed: g to find every match, i to ignore case, m to make ^ and $ work on each line.
  5. Try edge cases: empty lines, extra spaces, international formats, and lookalike data.
  6. When the highlights are exactly right, copy the pattern into your app or code.

Why local testing matters

The sample text you test against is often real data — customer emails, log files, order exports. Our Regex Tester runs in your browser, so that text never leaves your device. Many online testers send everything to a server.

Watch out for slow patterns

Some patterns with nested quantifiers, such as (a+)+, can take an extremely long time on certain inputs. If the tester ever freezes, simplify the pattern and avoid stacking + and * inside groups that repeat.

Quick regex cheat sheet

Keep this list handy. Each line shows the token and what it matches.

  • . — any single character except a new line
  • \d / \D — a digit / anything that isn't a digit
  • \w / \W — a letter, digit, or underscore / anything else
  • \s / \S — whitespace (space, tab, new line) / anything else
  • [abc] — any one of a, b, or c
  • [^abc] — any character except a, b, or c
  • [a-z] — any lowercase letter in the range
  • * — zero or more of the previous item
  • + — one or more of the previous item
  • ? — zero or one (makes the previous item optional)
  • {n} / {n,} / {n,m} — exactly n / at least n / between n and m
  • ^ / $ — start / end of the line or text
  • \b — a word boundary
  • (abc) — a group you can reuse or quantify
  • a|b — a or b
  • \ — escape a special character, such as \. for a literal dot

Real-world ways people use regex

You don't need to be a programmer to benefit from regex. Here are everyday jobs where a short pattern saves real time:

  • Cleaning spreadsheets: pull phone numbers, order IDs, or postcodes out of messy notes columns using REGEXEXTRACT in Google Sheets.
  • Find and replace in documents: editors like VS Code, Notepad++, and Sublime Text support regex in their search box, so you can reformat hundreds of dates in one go.
  • Validating forms: website sign-up forms often use regex to check that a postcode or phone number looks right before submission.
  • Searching logs: IT staff use regex to find every error line containing a certain code or IP address.
  • Renaming files: many bulk-rename tools accept regex, turning "IMG_0042.jpg" into "holiday-042.jpg" across a whole folder.
  • Filtering email: some mail clients and server rules support regex to sort messages automatically.

How to read a pattern you didn't write

When you find a regex online, read it from left to right, one piece at a time, and say each part out loud. Take ^[A-Z]{2}\d{2}$: start of text, exactly two capital letters, exactly two digits, end of text. So it matches "AB12" but not "ab12" or "AB123". Pasting a pattern into a tester and trying a few examples is the fastest way to confirm you understand it.

Start small

Build patterns gradually. Start with the simplest version that matches one example, then add pieces to handle variations. A pattern that grows step by step is much easier to debug than one written all at once.

Quick answers to common questions

Is regex the same in every language?

The basics are shared almost everywhere. Advanced features, such as lookbehind or named groups, vary slightly between JavaScript, Python, and other languages. Our tester uses JavaScript's regex engine, which matches what browsers use.

Should I use regex to validate email addresses?

Use a simple pattern to catch obvious typos, then confirm the address by sending an email. No regex can tell you whether an inbox actually exists.

Why does my dot match everything?

A dot is a wildcard. To match an actual period, escape it as \.

What does greedy mean?

Quantifiers grab as much as possible by default. Adding ? after them, as in .*?, makes them match as little as possible instead.

Can I use regex in Excel?

Newer versions of Microsoft 365 include REGEXTEST and related functions, and Google Sheets has REGEXMATCH, REGEXEXTRACT, and REGEXREPLACE.