Regular Expressions (Regex) for Beginners: A Practical Guide
Regular expressions (regex) are patterns used to match, search, and manipulate text. They are one of the most powerful tools in a developer's toolkit, but they can look intimidating at first. This guide breaks regex down into simple, understandable pieces with practical examples you can test right away using our Regex Tester.
What Are Regular Expressions?
A regular expression is a sequence of characters that defines a search pattern. Think of it as a "find" command on steroids. While a normal text search looks for an exact string, regex can match patterns like "any email address" or "a phone number in any format."
Regex is supported in virtually every programming language (JavaScript, Python, Java, C#, Go, PHP, Ruby) and many text editors (VS Code, Sublime Text, Notepad++).
Basic Syntax
Literal Characters
The simplest regex is just a plain string. The pattern cat matches the text "cat" wherever it appears.
Metacharacters
| Character | Meaning | Example | Matches |
|---|---|---|---|
. | Any character (except newline) | c.t | cat, cut, c9t |
^ | Start of string | ^Hello | "Hello world" |
$ | End of string | world$ | "Hello world" |
\d | Any digit (0-9) | \d\d\d | 123, 456 |
\w | Any word character | \w+ | hello, test123 |
\s | Any whitespace | hello\sworld | "hello world" |
Quantifiers
| Quantifier | Meaning | Example | Matches |
|---|---|---|---|
* | 0 or more | ab*c | ac, abc, abbc |
+ | 1 or more | ab+c | abc, abbc (not ac) |
? | 0 or 1 | colou?r | color, colour |
| {n} | Exactly n times | \d{3} | 123, 456 |
| {n,m} | Between n and m times | \d{2,4} | 12, 123, 1234 |
Character Classes
Square brackets define a set of characters to match:
[abc] → matches a, b, or c
[a-z] → matches any lowercase letter
[A-Z0-9] → matches any uppercase letter or digit
[^abc] → matches anything EXCEPT a, b, or c
Common Patterns
Here are regex patterns you can use right away. Test them with our Regex Tester:
Email Address
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URL
https?://[\w.-]+(?:\.[a-zA-Z]{2,})(?:/[\w./-]*)?
Phone Number (US)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Practical Tips
- Start simple: Build your pattern piece by piece, testing each step
- Use a tester: Always test with our Regex Tester before using patterns in production code
- Be specific: Avoid overly broad patterns that match more than intended
- Escape special characters: Characters like
.*+?must be escaped with\to match literally - Use flags: The
iflag makes patterns case-insensitive,gfinds all matches, andmenables multiline mode
Related Tools
Once you're comfortable with regex, explore these related tools:
- Text Replacement Tool — Find and replace text with regex support
- Remove Letters/Characters — Strip specific characters from text
Test Your Regex Patterns
Write, test, and debug regular expressions in real time with instant match highlighting.
Open Regex Tester →