Dev

How to Use Regex — A Beginner's Guide to Regular Expressions

8 min read  ·  Toolify Team

At first glance, a regular expression looks like someone fell asleep on the keyboard: ^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$. But behind the noise, regex is a precise and enormously powerful tool for searching, validating and transforming text. Once you understand the basic building blocks, it clicks quickly.

What Is a Regular Expression?

A regular expression (regex) is a pattern that describes a set of strings. It is used to search for matches within text, validate input (like email addresses or phone numbers), extract specific parts of a string, or perform find-and-replace operations. Regex is supported in virtually every programming language and in many text editors.

🔬 Practice as you read: Open Toolify's Regex Tester in a second tab and test each pattern from this guide as you go through it.

The Basics

Literal characters

The simplest regex is just plain text. The pattern cat matches the string "cat" anywhere in the input. It would match "caterpillar", "concatenate", and "black cat".

The dot — any character

A dot . matches any single character except a newline. So c.t matches "cat", "cut", "c3t", and even "c t".

Character classes

Square brackets define a character class — a set of characters, any one of which can match. [aeiou] matches any vowel. [a-z] matches any lowercase letter. [0-9] matches any digit. Add a caret inside the brackets to negate: [^aeiou] matches any character that is NOT a vowel.

Shorthand Character Classes

PatternMatchesEquivalent to
\dAny digit[0-9]
\DAny non-digit[^0-9]
\wWord character[a-zA-Z0-9_]
\WNon-word character[^a-zA-Z0-9_]
\sWhitespaceSpace, tab, newline
\SNon-whitespaceEverything else

Quantifiers — How Many Times?

Quantifiers control how many times the preceding element must appear:

QuantifierMeaning
*Zero or more times
+One or more times
?Zero or one time (optional)
{3}Exactly 3 times
{2,5}Between 2 and 5 times
{2,}At least 2 times

So \d+ matches one or more digits, \w{3} matches exactly three word characters, and colou?r matches both "color" and "colour".

Anchors — Where in the String?

^ matches the start of a string (or line in multiline mode). $ matches the end. \b matches a word boundary.

So ^hello only matches strings that begin with "hello", and world$ only matches strings that end with "world".

Groups and Alternation

Parentheses create a capturing group. (cat|dog) matches either "cat" or "dog" — the pipe | means "or". Groups are also used to apply quantifiers to a sequence: (ab)+ matches "ab", "abab", "ababab", and so on.

A Real-World Example: Email Validation

Let us build a basic email validation pattern step by step:

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

This matches "user@example.com" and "first.last@company.co.uk", and rejects "notanemail" or "missing@tld".

Flags

Flags modify how the pattern is applied:

Common Pitfalls

Next Steps

The best way to learn regex is to use it. Open the Regex Tester, pick a real-world problem — extracting dates from text, finding UK postcodes, validating phone numbers — and build your pattern one piece at a time. The built-in cheat sheet covers all the key syntax at a glance.