Regex for Beginners — Learn Regular Expressions
Regular expressions look intimidating at first. A pattern like ^[\w.-]+@[\w.-]+\.\w{2,}$ can seem like random punctuation. But regex is built from simple, logical pieces, and once you understand those pieces, you can read and write patterns confidently. This guide covers the fundamentals you need to start using regex today, and you can practice every example live in freeregextester.app.
What is a regular expression?
A regular expression (regex) is a sequence of characters that defines a search pattern. Programmers use regex to find, match, extract, and replace text within strings. Instead of searching for an exact word, regex lets you describe what you are looking for using rules and wildcards.
For example, instead of searching for the exact string "cat," you can write a pattern that matches "cat," "bat," and "hat" all at once: [cbh]at. The square brackets define a character class that matches any one of the letters inside.
Test String The cat sat on a hat near a bat
You can try this pattern right now in freeregextester.app to see the matches highlighted in real time.
What are the basic regex building blocks?
Every regex pattern is built from a small set of building blocks. Once you learn these, you can combine them to match almost any text pattern:
.— Matches any single character except a newline\d— Matches any digit (0-9)\w— Matches any word character (letters, digits, underscore)\s— Matches any whitespace (space, tab, newline)[abc]— Matches any one character in the brackets[^abc]— Matches any character NOT in the brackets*— Matches zero or more of the preceding element+— Matches one or more of the preceding element?— Matches zero or one of the preceding element{n}— Matches exactly n of the preceding element^— Matches the start of a line$— Matches the end of a line
Test String Call me at 555-1234 or 800-5678
How do I match email addresses with regex?
Matching email addresses is one of the most common regex tasks. A basic email pattern looks like this:
Test String Contact john@example.com or jane.doe@work.co.uk
Here is how it breaks down: [\w.-]+ matches one or more word characters, dots, or hyphens (the username). The @ matches the literal at symbol. Then [\w.-]+ matches the domain name, \. matches a literal dot, and \w{2,} matches two or more word characters for the top-level domain.
Paste this pattern into freeregextester.app to test it against your own email strings.
How do I test my regex patterns?
The fastest way to test regex is to use a live tester like freeregextester.app. Type your pattern in the regex input, paste your test text below, and see matches highlighted instantly. There is no delay because everything runs locally in your browser using JavaScript's native regex engine.
Start with simple patterns and gradually add complexity. For example, begin with a literal string, then add character classes, then quantifiers. Testing each step lets you see exactly what each piece of your pattern does. freeregextester.app also shows capture groups, so you can verify that your parenthesized groups are extracting the right substrings.
Practice regex right now
Open freeregextester.app and start testing patterns with instant visual feedback. No sign-up needed.
Open Regex Tester