Developer Tools

Regular Expressions (Regex) Tutorial for Beginners

PAYATE Team· · 5 min read ·Updated Oct 18, 2026
Regular Expressions (Regex) Tutorial for Beginners

Regular expressions look like someone fell asleep on the keyboard — ^\d{3}-\d{4}$ — but once you learn to read them, they become one of the most powerful tools a developer, data analyst or even a careful spreadsheet user can have. This beginner's tutorial explains what a regex is, the core building blocks, and how to test your patterns safely before you use them.

What is a regular expression?

A regular expression (regex or regexp) is a pattern that describes a set of strings. Instead of searching for one exact word, you describe the shape of what you want — "three digits, a dash, then four digits" — and the regex engine finds every piece of text that matches. Regex is built into almost every programming language, text editor and command-line tool, so the skill transfers everywhere. The best way to learn is interactively: keep the PAYATE Regex Tester open and try each example as you read, with live match highlighting.

Literal characters and metacharacters

At its simplest, a regex matches exactly what you type. The pattern cat matches the letters c-a-t anywhere in the text. The power comes from metacharacters — symbols with special meaning:

  • . matches any single character.
  • \d matches any digit (0–9), and \D matches any non-digit.
  • \w matches any "word" character (letters, digits, underscore); \W is the opposite.
  • \s matches whitespace (space, tab, newline); \S is non-whitespace.

If you want to match a metacharacter literally — say an actual dot — you "escape" it with a backslash: \. matches a full stop rather than "any character."

Character classes: choosing from a set

Square brackets define a character class — a set of characters, any one of which can match:

  • [aeiou] matches any single vowel.
  • [a-z] matches any lowercase letter, using a range.
  • [A-Za-z0-9] matches any letter or digit.
  • [^0-9] — a caret inside the brackets negates the set, so this matches anything that is not a digit.

Quantifiers: how many times

Quantifiers say how many times the thing before them may repeat:

  • * — zero or more times.
  • + — one or more times.
  • ? — zero or one time (optional).
  • {3} — exactly three times.
  • {2,4} — between two and four times.
  • {2,} — two or more times.

So \d{3}-\d{4} from the opening means "three digits, a dash, four digits" — a classic phone-number fragment. And colou?r matches both "color" and "colour" because the u is optional.

Anchors and boundaries

Anchors do not match characters; they match positions:

  • ^ anchors to the start of the string (or line).
  • $ anchors to the end.
  • \b matches a word boundary — the edge between a word character and a non-word character.

The difference matters. cat matches "cat" inside "category," but \bcat\b matches only the standalone word "cat." And ^\d{3}-\d{4}$ matches a string that is exactly that pattern and nothing else — useful for validating a whole input field.

Groups and alternation

Parentheses create a group, which lets you apply a quantifier to several characters at once and "capture" the matched part for later use. Capturing is what powers find-and-replace: you grab pieces of the match and rearrange them. The pipe | means "or":

  • (ab)+ matches "ab," "abab," "ababab," and so on.
  • (jpg|png|gif) matches any one of those three extensions.
  • (\d{4})-(\d{2})-(\d{2}) captures a date's year, month and day as three separate groups.

If you need the grouping but do not want to capture, use a non-capturing group: (?:...).

A worked example: validating an email shape

Let's read a simplified email pattern: ^[\w.+-]+@[\w-]+\.[\w.-]+$

  • ^ — start of string.
  • [\w.+-]+ — one or more word characters, dots, plus or hyphens (the part before the @).
  • @ — a literal at-sign.
  • [\w-]+ — the domain name.
  • \.[\w.-]+ — a dot followed by the top-level domain.
  • $ — end of string.

This is deliberately loose — fully validating every legal email with regex is famously hard, which is why for real forms you should pair a simple pattern with an actual check. The Email Validator does exactly that, and even suggests fixes for common typos.

Common beginner mistakes

  • Greedy matching. By default .* grabs as much as possible. If you match <.*> against <a><b> you get the whole thing, not just <a>. Add a ? to make it "lazy": <.*?>.
  • Forgetting to escape. Dots, question marks and parentheses are special; escape them with a backslash when you mean them literally.
  • Trying to parse HTML with regex. Regex is for patterns, not for nested structures. Use a proper parser for HTML and JSON.
  • Not testing edge cases. Always try empty input, extra spaces and near-misses.

Flags change how a pattern behaves

Most engines let you add flags that modify the whole match. The most common are the case-insensitive flag, so cat also matches "CAT" and "Cat"; the global flag, which finds every match instead of stopping at the first; and the multiline flag, which makes ^ and $ match the start and end of each line rather than the entire string. Knowing these three saves a lot of confusion — a pattern that "does not work" is often just missing the case-insensitive or global flag. In most languages you set them when you compile the expression, and a good tester lets you toggle them so you can see the effect instantly.

Where to use your regex

Once your pattern works, you can use it almost anywhere: in your code's search or validation, in a text editor's find-and-replace, in command-line tools like grep, and in data-cleaning. For bulk text cleanup with match-case, whole-word and regex options, try the Find and Replace tool, and for tidying lines of data the Text Tools & Cleaner.

Key takeaways

A regular expression describes the shape of text using literals, character classes like [a-z], quantifiers like + and {2,4}, anchors like ^ and $, and groups with (...) and |. Start small, build up, and always test. Keep the Regex Tester open as you learn — seeing your matches highlighted live is the fastest way to make regex click.

Try the tools

P
Written by PAYATE Team

The PAYATE team builds free tools for merchants and developers.

More PAYATE tools