How to Use Regex: A Beginnerâs Guide to Regular Expressions
Regular expressions look intimidating at first glance, but they are one of the most powerful tools in any developerâs toolkit. This guide will take you from zero to writing your own patterns in minutes.
What Is Regex and Why Should You Learn It?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Think of it as a mini programming language designed specifically for finding, matching, and manipulating text.
Regex is used everywhere:
- Form validationâ checking if an email address, phone number, or URL is correctly formatted before submitting.
- Search and replaceâ finding all dates in a document and reformatting them, or stripping HTML tags from text.
- Log analysisâ extracting IP addresses, error codes, or timestamps from server log files.
- Data cleaningâ removing extra whitespace, standardizing phone numbers, or extracting structured data from unstructured text.
- Code refactoringâ renaming variables, updating function calls, or migrating API patterns across a large codebase.
Once you learn regex, you will find yourself reaching for it constantly. It works in virtually every programming language (JavaScript, Python, Java, Go, PHP, Ruby) and in tools like VS Code, Sublime Text, grep, sed, and awk.
Literal Characters: The Simplest Pattern
The simplest regex is just a plain string. The pattern hellomatches the exact text âhelloâ wherever it appears. No special syntax needed.
But regex becomes powerful when you add metacharactersâ special characters that have meaning beyond their literal value.
Metacharacters: The Building Blocks
These are the core special characters you need to know:
| Character | Meaning | Example |
|---|---|---|
. | Any single character (except newline) | c.t matches âcatâ, âcotâ, âcutâ |
^ | Start of string (or line with m flag) | ^Hello matches âHello worldâ but not âSay Helloâ |
$ | End of string (or line) | world$ matches âHello worldâ |
\ | Escape a special character | \. matches a literal period |
| | OR (alternation) | cat|dog matches âcatâ or âdogâ |
Character Classes: Matching Categories of Characters
Character classes let you match any character from a defined set. You write them inside square brackets:
[abc]â matches âaâ, âbâ, or âcâ[a-z]â matches any lowercase letter from a to z[A-Z]â matches any uppercase letter[0-9]â matches any digit[^abc]â matches any character except a, b, or c (the^inside brackets means ânotâ)
Regex also provides shorthand character classes that save typing:
\dâ any digit (same as[0-9])\wâ any âwordâ character: letters, digits, or underscore (same as[a-zA-Z0-9_])\sâ any whitespace character: space, tab, newline\D,\W,\Sâ the negated versions (non-digit, non-word, non-whitespace)
Quantifiers: How Many Times to Match
Quantifiers control how many times a character or group should repeat:
| Quantifier | Meaning | Example |
|---|---|---|
* | Zero or more times | go*d matches âgdâ, âgodâ, âgoodâ, âgooodâ |
+ | One or more times | go+d matches âgodâ, âgoodâ but not âgdâ |
? | Zero or one time (optional) | colou?r matches âcolorâ and âcolourâ |
{n} | Exactly n times | \d{4} matches exactly 4 digits like â2026â |
{n,m} | Between n and m times | \d{2,4} matches 2, 3, or 4 digits |
By default, quantifiers are greedyâ they match as much text as possible. Add a ? after the quantifier to make it lazy (match as little as possible). For example, .+? matches one or more characters, but stops at the earliest opportunity.
Groups and Captures
Parentheses create groups that let you apply quantifiers to entire sub-patterns and extract matched text:
(abc)â a capturing group. Matches âabcâ and remembers the match so you can reference it later (e.g., in replacements).(?:abc)â a non-capturing group. Groups the pattern for quantifiers or alternation but does not store the match.
Practical example: The pattern (\d{4})-(\d{2})-(\d{2})matches a date like â2026-03-05â and captures the year, month, and day into separate groups. In a replacement, you could rearrange them: $3/$2/$1would produce â05/03/2026â.
Flags: Modifying Regex Behavior
Flags are added after the closing delimiter of a regex to change how the engine processes the pattern:
g(global) â find all matches in the string, not just the first one.i(case-insensitive) â treat uppercase and lowercase letters as equivalent./hello/imatches âHelloâ, âHELLOâ, and âhElLoâ.m(multiline) â make^and$match the start and end of each line, not just the entire string.
In JavaScript, you write them after the regex literal: /pattern/gi. In Python, you pass them as arguments: re.findall(pattern, text, re.IGNORECASE).
Real-World Regex Examples
Here are patterns you can use in real projects. Try them in our free Regex Tester to see them in action.
Email Address (Basic Validation)
^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$Matches strings like [email protected] or [email protected]. Note: email validation is notoriously complex. This pattern covers most common cases but is not RFC 5322 compliant. For production use, combine regex with server-side verification.
URL
https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})[\/\w.-]*\/?Matches HTTP and HTTPS URLs like https://www.bontello.com/tools. The s?makes the âsâ in âhttpsâ optional.
Phone Number (US Format)
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$Matches formats like (555) 123-4567, 555-123-4567, and 5551234567. The ? after each separator group makes parentheses, dashes, dots, and spaces optional.
HTML Tag
<\/?[a-zA-Z][\w]*[^>]*>
Matches opening and closing HTML tags like <div class="box"> or </p>. Useful for stripping HTML from text, though for full HTML parsing you should use a proper DOM parser.
Tips for Writing Better Regex
- Start simple. Write a pattern that matches your specific case first, then generalize it. Do not try to handle every edge case at once.
- Test with real data. Paste actual sample text into a regex tester and verify your pattern matches what you expect (and does not match what it should not).
- Use non-capturing groups when you do not need captures. Writing
(?:...)instead of(...)is cleaner and slightly more efficient. - Be specific. Using
\dinstead of.when you expect digits makes your pattern more readable and reduces false matches. - Comment complex patterns. Most regex engines support a verbose mode (the
xflag) that lets you add comments and whitespace for readability. - Know when not to use regex. Parsing nested structures (like HTML or JSON) with regex is fragile. Use a proper parser for those tasks.
Start Practicing
The best way to learn regex is by doing. Start with a simple pattern, test it against sample text, tweak it, and build from there. Within an hour of practice, you will be comfortable with the fundamentals covered in this guide.
Ready to try your first pattern? Open our free Regex Tester â paste your text, type a pattern, and see matches highlighted in real time. No signup required.
Essayez maintenant â Gratuit
Utilisez notre Regex Tester directement dans votre navigateur. Aucune inscription, aucun envoi vers un serveur.
Ouvrir Regex Tester