What Is a Regular Expression?
A regular expression (regex) is a sequence of characters that defines a search pattern. They're used for validating input, finding and replacing text, extracting data from strings, and parsing logs. Nearly every programming language has built-in regex support.
How to Test Regex Online
Use the tool.tl Regex Tester:
- Go to tool.tl/regex-tester
- Enter your pattern in the Regex field (e.g.,
\d+) - Enter test text in the input area
- Matches highlight in real time
- See capture groups and match details below
Regex Syntax Quick Reference
Character Classes
| Pattern | Meaning | Example |
|---|---|---|
. | Any character except newline | a.c → abc, a1c |
\d | Digit [0-9] | \d+ → 123 |
\w | Word char [a-zA-Z0-9_] | \w+ → hello_world |
\s | Whitespace (space, tab, newline) | \s+ → spaces |
\D \W \S | Negations of above | Non-digit, non-word, non-space |
[abc] | Character set: a or b or c | [aeiou] → vowels |
[^abc] | Negated character set | [^0-9] → non-digit |
Quantifiers
| Quantifier | Meaning |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{n} | Exactly n times |
{n,m} | Between n and m times |
{n,} | n or more times |
Anchors & Boundaries
| Anchor | Meaning |
|---|---|
^ | Start of string (or line with multiline flag) |
$ | End of string (or line) |
\b | Word boundary |
\B | Non-word boundary |
Ready-to-Use Regex Patterns
# Email address
^[\w.+-]+@[\w-]+\.[\w.]{2,}$
# US phone number
^(\+1)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
# IPv4 address
^(\d{1,3}\.){3}\d{1,3}$
# URL (http/https)
https?://[\w\-.]+(:\d+)?(/[\w\-./?%&=#]*)?$
# Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
# Hex color code
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
# Positive integer
^[1-9]\d*$
# Strong password (min 8 chars, upper+lower+digit+special)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
# US ZIP code
^\d{5}(-\d{4})?$
# Credit card number (basic)
^\d{13,19}$
Capture Groups
Parentheses () create capture groups to extract specific parts of a match: