Regex Tutorial for Beginners
Regular expressions, often called regex, are patterns used to match, search, validate, and transform text. They can look cryptic at first, but most useful patterns are built from a small set of ideas: literals, character classes, quantifiers, groups, anchors, and flags.
Use the Regex Tester when you want to test a pattern against sample text before using it in code, a spreadsheet, a log search, or a data cleanup workflow.
What Is Regex?
A regex is a sequence of characters that defines a search pattern. For example, the pattern hello matches the exact text “hello”. A more flexible pattern like h.llo can match “hello”, “hallo”, or “hxllo” because the dot represents one character.
Regex is commonly used for:
- Validating simple input formats
- Finding text inside logs or documents
- Extracting IDs, dates, slugs, or URLs
- Replacing repeated text patterns
- Cleaning copied data before import
Basic Syntax
Literals
Most characters match themselves. The pattern hello matches the text “hello”.
Special Characters
.- Matches any single character except a line break in many engines*- Matches zero or more of the previous token+- Matches one or more of the previous token?- Matches zero or one of the previous token^- Matches the start of a string or line$- Matches the end of a string or line
If you need to match one of these characters literally, escape it with a backslash. For example, \. matches a real dot.
Character Classes
[abc]- Matches a, b, or c[a-z]- Matches any lowercase letter[0-9]- Matches any digit\d- Matches a digit\w- Matches a word character in many engines\s- Matches whitespace
Character classes are helpful when the exact character can vary but still belongs to a known group.
Common Patterns
| Pattern | Matches |
|---|---|
\d{3}-\d{4} | Phone numbers like 123-4567 |
\b[A-Z]{2}\d{4}\b | Codes like AB1234 |
https?://\S+ | HTTP and HTTPS URLs |
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b | Simple IP-like strings |
For email validation, avoid trying to write a perfect regex by hand. Email rules are more complicated than they look, and most applications are better served by a pragmatic check plus confirmation email.
Quantifiers
{n}- Exactly n times{n,m}- Between n and m times{n,}- At least n times
For example, \d{2,4} matches 2, 3, or 4 digits. Quantifiers are powerful, but they can also make a pattern too broad if you do not anchor it or limit the surrounding context.
Anchors and Whole-Value Validation
Beginners often write a regex that finds a pattern somewhere inside a string when they actually need to validate the whole value.
For example:
\d{5}This finds five digits inside abc12345xyz. If you need the entire value to be exactly five digits, anchor it:
^\d{5}$Anchors are important for form validation, IDs, short codes, and import rules. Without anchors, a pattern may pass values that merely contain a valid-looking fragment.
Groups and Captures
Parentheses create capture groups. (hello) (world) captures “hello” in group 1 and “world” in group 2.
Use (?:...) for non-capturing groups when you need grouping without saving the matched value. This makes complex patterns easier to maintain and avoids unnecessary capture results.
Greedy vs Lazy Matching
Many quantifiers are greedy by default, meaning they match as much as possible. In HTML-like text, this can be surprising:
<.+>Given <strong>bold</strong>, this may match the entire string instead of only the opening tag. A lazy version uses ? after the quantifier:
<.+?>Lazy matching is useful, but the better long-term fix is often a more specific pattern.
Build Patterns Incrementally
Do not write a complex regex in one step. Start with the smallest useful part, test it, then add one idea at a time.
For a date-like string such as 2026-07-03, you might build:
\d{4}
\d{4}-\d{2}
^\d{4}-\d{2}-\d{2}$This makes mistakes easier to find. If the final pattern fails, you know which step introduced the problem.
Also keep a small set of examples beside the pattern:
- values that should match
- values that should not match
- edge cases such as empty strings, extra spaces, or different separators
This habit prevents regex from becoming mysterious code that nobody wants to touch later.
Flags
g- Global, find all matchesi- Case insensitivem- Multiline anchors
Different programming languages support different flags and syntax details, so always test your pattern in the same environment where it will run.
Practical Tips
- Always test your regex with realistic examples.
- Include both matching and non-matching samples.
- Use anchors when the entire value must match.
- Use non-capturing groups when you do not need the captured value.
- Be careful with broad patterns like
.*. - Keep complex patterns documented near the code that uses them.
Regex Is Not Always the Right Tool
Regex is great for pattern matching, but it is not a full parser. Avoid using one large regex to parse nested HTML, complex JSON, programming languages, or every possible email address. Use a proper parser or validator when the format has nested structure or complicated rules.
A good rule: use regex for simple, local text patterns. Use specialized tools for structured data. For example, use the JSON Formatter & Validator for JSON syntax and the HTML Formatter for markup review instead of trying to parse those formats with regex alone.
Test Your Regex
Use our free Regex Tester with real-time highlighting and match details to debug your expressions before deploying them. If the pattern will be pasted into a JavaScript string, the String Escape / Unescape can help you check escaping.
Conclusion
Regex is a practical skill that improves with small, real examples. Start with readable patterns, test them against edge cases, and avoid making one expression solve more than it should.
Related QuickToolFlow Tools
- Regex Tester for testing patterns with real-time matches.
- String Escape / Unescape for preparing strings before using them in code.
- Text Diff Checker for comparing regex output or edited text.
Keep going