Regex, Explained Simply: A Beginner's Guide to Pattern Matching

By Asahluma Tyika

If you’ve ever seen a string of symbols like ^\S+@\S+\.\S+$ in someone’s code and quietly closed the tab, this post is for you. Regular expressions (regex) look intimidating, but they’re built from a small set of simple ideas stacked together. Once you know the eight or so symbols that do most of the work, you can read almost any regex you’ll encounter in the wild.

This tutorial assumes zero prior regex experience. If you can write a basic if statement, you’re ready.


What Regex Actually Is

A regex pattern doesn’t search for exact words — it searches for a shape. Instead of telling your program “find the text asa@teekayx.com,” you tell it “find some characters, then an @, then more characters, then a ., then more characters.” Regex will match that shape wherever it shows up, regardless of what the actual letters and numbers are.

This is why regex is everywhere: form validation, search-and-replace, log file parsing, data cleaning. Anywhere you need to find or verify a pattern in text, regex is the tool.

Here’s the core idea in practice. Given the input:

contact: asa@teekayx.com

and the pattern \S+@\S+\.\S+, regex scans the string and pulls out asa@teekayx.com — the part that matches the shape. It ignores the word “contact:” entirely, because that part doesn’t fit the pattern.


Part 1: Symbols That Stand In For a Character

These three symbols represent a single character, but instead of matching one exact letter, they match a category of characters.

Symbol Meaning Example
\d any single digit matches 0, 1, 29
\w any letter, digit, or underscore matches a, Z, 7, _
. literally any character matches a, 5, #, even a space

Scenario: You’re building a 4-digit PIN checker.

pattern: \d\d\d\d
input:   4821
result:  match — four digits found, in order

Each \d consumes exactly one digit. Four of them in a row means “exactly four digits, nothing else.” If the input were 482 (only three digits) or 48a1 (a letter in the middle), it would not match.


Part 2: Symbols That Say How Many

Matching one character at a time gets tedious fast. These three symbols control how many times the thing before them should repeat.

Symbol Meaning Example
+ one or more of what came before \d+ matches 7 or 789
* zero or more of what came before \w* matches nothing at all, or abc
? optional — zero or exactly one colou?r matches color or colour

Scenario: A user types their username on signup.

pattern: \w+
input:   asa_dev99
result:  match — one or more letters/digits/underscore

\w+ doesn’t care how long the username is — it just needs at least one valid character. This is the difference between + and *: + requires at least one match, * is happy with zero.


Part 3: Symbols That Pin Down Position

So far, every pattern above could match a fragment buried anywhere inside a longer string. That’s often not what you want — especially for validation, where you need the entire input to fit the pattern, not just part of it.

Symbol Meaning Example
^ the match must start right here ^Hi matches the start of Hi there
$ the match must end right here bye$ matches the end of good bye

Scenario: Checking if a filename is a complete .png path.

pattern: ^\w+\.png$
input:   banner.png
result:  match — starts and ends exactly right

Without the ^ and $, the pattern \w+\.png would also match banner.png.exe (because banner.png appears somewhere inside it) — which is almost certainly not what you want when validating a file type. Anchoring both ends closes that gap.


Now You Can Read This

Here are the same four patterns from the “screenshot this” cheat sheet — and now every symbol in them is one you’ve already learned.

Email address

^\S+@\S+\.\S+$

Start of string, one or more non-space characters, an @, more non-space characters, a literal ., more non-space characters, end of string.

Phone number

^\d{3}-\d{3}-\d{4}$

Exactly three digits, a dash, three more digits, a dash, four more digits, and nothing else. (The {3} and {4} are shorthand for “exactly this many times” — a small extension of the same “how many” idea from Part 2.)

Starts with http:// or https://

^https?://

The ? makes the s optional, so this matches both http:// and https://, as long as it appears at the very start of the string.

Only letters, numbers, and underscores

^[A-Za-z0-9_]+$

The square brackets define a custom set of allowed characters — this is the one new idea not covered above, and it’s worth learning next once the basics feel comfortable.


Try It Yourself

You don’t need to install anything to practice. Open regex101.com, paste in some sample text, and type a pattern from this post into the pattern field. It highlights matches live, which makes it much easier to build intuition than reading about it alone.

A good first exercise: take the email pattern above, and try breaking it. Remove the ^, then test it against notanemail@fake — does it still “match” something it shouldn’t?


What’s Next

This covers the 90% of regex you’ll actually use in day-to-day form validation and text searching. If you want to go further, the next concepts worth learning are:

  • Character classes ([A-Za-z0-9_]) — defining your own custom set of allowed characters
  • Groups ((...)) — capturing part of a match to reuse or extract later
  • Alternation (|) — matching one pattern or another

We’ll cover those in a follow-up post. For now, bookmark this page — every pattern here is one you’ll reach for again.

Share: X (Twitter) Facebook LinkedIn