Regular expressions look like keyboard accidents, but they are one of the most practical skills in computing: a compact way to describe a pattern and find every piece of text that matches it. Here are 20 patterns you will actually use, each explained in plain English.

What a regex actually is

A regular expression, usually shortened to regex, is a search pattern written in a small special language. Instead of searching for one exact word, you describe the shape of what you want: three digits, then a dash, then four digits, for example. The computer then finds every match. Regex appears in text editors, spreadsheets, programming languages, form validation, and log-file searches, so learning it once pays off in many places.

Every pattern is built from a few building blocks. A plain character matches itself, so cat matches the letters c-a-t. A dot matches almost any single character. Square brackets define a set, so [0-9] matches any single digit and [A-Za-z] matches any single letter. Quantifiers say how many times something may repeat: * means zero or more, + means one or more, ? means zero or one, and {3} means exactly three times. Anchors pin a match to the start or end of a string: ^ for the start and $ for the end. With just those pieces, you can read most of the patterns below.

How to read a pattern, piece by piece

Take the classic United States phone pattern ^\d{3}-\d{3}-\d{4}$. Reading left to right: the caret says the match must start here, \d{3} means exactly three digits, the dash is a literal dash, \d{3} is three more digits, another dash, then \d{4} is four digits, and the dollar sign says the match must end here. Without the anchors, the same pattern would happily match the middle of a longer number, which is rarely what you want for validation.

Patterns for validation

Validation patterns check whether input has the right shape before you accept it. They are almost always anchored with ^ and $, because a partial match is not a valid entry.

Patterns for numbers, dates, and times

These patterns pull structured values out of messy text, which is where regex earns its keep in data cleanup.

Patterns for finding and cleaning up text

Not every pattern validates a whole field. Many simply find or tidy text, and these are the ones writers, editors, and spreadsheet users reach for most.

Worked example: validating a signup form

Imagine a signup form with three fields: username, email, and ZIP code. Without validation, a user can submit a one-character username, an email with no @ sign, and a ZIP code containing letters, and the errors surface later as bounced mail and failed deliveries. With three anchored patterns, each field is checked the moment it is entered: username against ^[A-Za-z0-9_]{3,16}$, email against the simple email pattern above, and ZIP code against ^\d{5}(-\d{4})?$. That is the practical payoff: small patterns, placed early, prevent large cleanup jobs later.

Three mistakes that break most patterns

First, forgetting to escape special characters. A dot, a parenthesis, and a plus sign all have special meanings, so a pattern meant to match a literal price like 9.99 needs the dot escaped as \., or it will also match 9x99. Second, forgetting anchors on validation patterns, which lets a valid substring inside invalid input pass. Third, using a greedy .* where a specific class belongs. When a pattern matches too much, replace the dot with the narrowest class that fits, such as [^<]* for text inside a tag, and the over-matching usually disappears.

AdvertisementAd slot: in-content

Frequently asked questions

Do I need to memorize regex to use it well?

No. Most people reuse a small set of patterns and look up the rest. Learn the handful of symbols in this guide, keep a cheat sheet nearby, and test every pattern against real examples before you trust it. Recognition beats memorization.

Why does my pattern match too much text?

The usual cause is a greedy quantifier. By default, symbols like * and + grab as much as possible, so .* can swallow an entire line. Add a question mark to make it lazy, as in .*?, or narrow the character class so it can only match what you actually want.

What is the difference between \d, \w, and \s?

The symbol \d matches any digit, \w matches a word character such as a letter, digit, or underscore, and \s matches whitespace such as a space, tab, or newline. Their uppercase opposites, \D, \W, and \S, match everything except those categories. Together they cover most everyday matching.

Is regex the right tool for validating an email address?

For a quick sanity check, yes. A simple pattern catches typos like a missing @ sign or domain. For strict validation, no pattern is perfect, because the full email specification is enormous. Use a simple pattern plus a confirmation email for anything important.