I am going to be honest with you. The first time I saw a regular expression, I thought someone’s code editor had a stroke. It looked like this:
^(?:[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$
And I closed the tab. Walked away from my computer. Made a sandwich. Came back thirty minutes later and still had no idea what any of it meant.
But here is the thing. I eventually learned regex. And it was not nearly as bad as that first impression suggested. The problem is that every regex tutorial starts with the hardest stuff. They show you these monster patterns and say “regex is powerful!” Yeah, powerful at making me feel stupid.
So this is the guide I wish someone had given me. We are starting small. Really small. And by the end of this, you will be writing your own patterns without breaking a sweat.
What Even Is Regex?
Regex is short for “regular expressions.” It is a tiny language for describing patterns in text. That is literally all it is. You describe what you are looking for, and the computer finds every match.
Think about the Find function in Word or Google Docs. You type a word, it highlights that word everywhere in the document. Regex is like that, but on protein shakes. Instead of searching for one specific word, you can search for a pattern. Like “any word that starts with a capital letter” or “anything that looks like a phone number” or “every line that ends with a period.”
Why does this matter? Because you probably spend time doing things like:
- Scrolling through a document looking for email addresses
- Manually checking if phone numbers are formatted correctly
- Finding every URL in a block of text
- Removing duplicate words from a list
- Replacing dates from one format to another
Regex does all of that in seconds. Not approximately. Literally seconds.
Your First Regex Pattern (It Is Just Letters)
Here is your first regex pattern:
cat
That is it. The regex pattern cat matches the word “cat” in any text. It will also match “cat” inside words like “concatenate” and “catalog,” but we will deal with that later. For now, just know that the simplest regex is just the text you want to find.
Paste some text into Regex Tester and try it. Type cat in the pattern box. Every occurrence of “cat” in your text lights up. Congratulations, you just used regex. It was not scary at all. You are doing great.
The Dot: Match Any Character
The first special character you need to know is the dot: .
A dot matches any single character. So the pattern c.t matches “cat” and “cut” and “cot” and “c9t” and “c!t” and literally anything that has a “c” then one character then a “t.”
This is useful when you are not sure what character is in a specific position, or when you do not care. Maybe you are looking for a product code that starts with “AB” and ends with “5” but the middle character varies. The pattern AB.5 finds all of them.
Square Brackets: Pick From a List
What if you only want to match specific characters? Square brackets let you define a set.
[aeiou] matches any single vowel. So c[aeiou]t matches “cat” and “cot” and “cut” but not “cbt” or “c9t.”
You can also use ranges:
[a-z]matches any lowercase letter[A-Z]matches any uppercase letter[0-9]matches any digit[a-zA-Z]matches any letter, upper or lower[a-zA-Z0-9]matches any letter or digit
This is already enough to do useful stuff. The pattern [A-Z][a-z]+ matches any word that starts with a capital letter. That is how you find proper nouns in a block of text.
Quantifiers: How Many?
Now we need to talk about how many times something should appear. These are called quantifiers.
*means “zero or more times”+means “one or more times”?means “zero or one time” (basically means “optional”){3}means “exactly 3 times”{2,5}means “between 2 and 5 times”
So [0-9]+ means “one or more digits.” It matches “5” and “42” and “99999.” This is how you find numbers in text, no matter how many digits they have.
colou?r matches both “color” and “colour” because the u is optional. If you have ever argued about American vs British spelling, regex does not take sides. It matches both.
[0-9]{3}-[0-9]{4} matches a phone number pattern like “555-1234.” Three digits, a hyphen, four digits. You already know enough to match phone numbers. I told you this was not that bad.
The Backslash Shortcuts
Typing [0-9] every time you want a digit is annoying. So regex has shortcuts:
\dmeans “any digit” (same as[0-9])\wmeans “any word character” (same as[a-zA-Z0-9_])\smeans “any whitespace” (spaces, tabs, line breaks)
The uppercase versions mean the opposite:
\Dmeans “anything that is NOT a digit”\Wmeans “anything that is NOT a word character”\Smeans “anything that is NOT whitespace”
So \d{3}-\d{3}-\d{4} matches a US phone number like “555-123-4567.” And \w+@\w+\.\w+ is a rough (very rough) email pattern. We will make it better later, but it catches most emails.
Wait. Did you notice the \. in that email pattern? The backslash before the dot means “a literal dot, not the wildcard dot.” Because without the backslash, the dot would match any character. When you need an actual period, you escape it with a backslash: \.
This applies to all special characters. If you need a literal +, write \+. A literal * is \*. A literal [ is \[. You get the idea.
Anchors: Start and End
Sometimes you do not just want to find a pattern anywhere. You want to match it at the start or end of a line.
^means “the start of the line”$means “the end of the line”
So ^Hello only matches “Hello” if it is at the very beginning of a line. “Say Hello” would NOT match because “Hello” is not at the start.
And \.$ matches a period at the end of a line. Useful for finding sentences that end properly.
^\d matches lines that start with a number. Handy for finding numbered lists in messy text.
Groups: Capture Pieces
Parentheses create groups. This is where regex gets really practical for find-and-replace operations.
The pattern (\d{4})-(\d{2})-(\d{2}) matches a date like “2026-08-15.” But it also captures three groups: the year, the month, and the day. In a find-and-replace, you can reference these groups with $1, $2, and $3.
So if you want to convert dates from YYYY-MM-DD to MM/DD/YYYY, you search for (\d{4})-(\d{2})-(\d{2}) and replace with $2/$3/$1. That swaps the pieces around. Every date in your document gets reformatted in one shot. This is the kind of thing that would take you an hour to do manually for a 200 line spreadsheet. Regex does it in one click.
Try this in Find and Replace with regex mode turned on. Paste some dates, enter the pattern, and watch them all transform. It feels like magic the first time.
The Pipe: Either/Or
The | character means “or.” So cat|dog matches “cat” or “dog.” Simple.
You can combine this with groups: (Mr|Mrs|Ms|Dr)\.?\s\w+ matches any name with a title prefix. It finds “Mr. Smith” and “Dr Johnson” and “Ms. Williams” all at once.
This is incredibly useful for cleaning up data that has multiple formats for the same thing. Different date formats, different phone formats, different abbreviations. One pattern catches them all.
Real World Patterns You Can Actually Use
Okay, enough theory. Here are patterns you can copy and paste right now into Regex Tester or Regex Extractor and use immediately.
Find All Email Addresses
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
This catches pretty much every standard email format. Paste a messy document and extract every email in one go.
Find All URLs
https?://[^\s<>"]+
Matches any URL starting with http or https. The ? after the s makes the “s” optional, so it catches both http and https.
Find US Phone Numbers (Multiple Formats)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
This matches “555-123-4567” and “(555) 123-4567” and “555.123.4567” and “555 123 4567.” All common formats, one pattern.
Find Lines That Are Empty or Only Have Spaces
^\s*$
Matches blank lines. Use this in find-and-replace to delete all empty lines from a document. Replace the match with nothing.
Remove Duplicate Words (Like “the the”)
\b(\w+)\s+\1\b
This finds any word that appears twice in a row, like “the the” or “is is.” The \1 refers back to whatever the first group matched. Replace with $1 to keep just one copy.
Extract Numbers from Text
\d+\.?\d*
Matches whole numbers and decimal numbers. Finds “42” and “3.14” and “99999.” Paste a document full of text and pull out every number.
The Pattern Building Process
When you need a new regex pattern, here is my actual process:
-
Write down exactly what you are looking for in plain English. “I need all phone numbers that start with 555.”
-
Break it into pieces. “The literal text 555, then a separator (dash or space or dot), then 3 digits, then another separator, then 4 digits.”
-
Translate each piece into regex.
555[-.\s]\d{3}[-.\s]\d{4} -
Test it in Regex Tester with real sample data.
-
Tweak until it catches everything and nothing extra.
That is it. You do not memorize patterns. You build them from pieces. Every single time.
What Regex Cannot Do
I want to be upfront about this because nobody tells beginners the limitations.
Regex is bad at parsing nested structures. It cannot reliably match HTML tags that contain other HTML tags. It cannot parse JSON or XML properly. It is not built for that. If you need to parse structured data, use a proper parser.
Regex is also bad at understanding meaning. It can find patterns, but it does not know what words mean. It cannot tell if “bank” refers to a financial institution or the side of a river.
And complex regex patterns can be slow. If you write a sloppy pattern and run it on a 10 MB file, it might take forever. Or crash. This is called catastrophic backtracking and it is a real problem. Keep your patterns simple and test them on small data first.
Where to Go From Here
You now know enough regex to handle most common text processing tasks. Seriously. The basics we covered handle something like 85% of what normal people need.
If you want to practice, here are some tasks to try:
-
Go to Regex Tester and paste some sample text. Try to match all the email addresses, then all the phone numbers, then all the URLs.
-
Go to Find and Replace, turn on regex mode, and try reformatting some dates from one format to another.
-
Go to Regex Extractor and extract all the numbers from a block of text.
Every time you use regex, it gets a little more natural. Like riding a bike. Except the bike has weird symbols all over it. And sometimes it falls over for no reason. But mostly it works great and saves you a ton of time.
You got this.