What is a Regular Expression?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex is used to find, match, validate, and replace text based on patterns rather than exact strings. It is supported in virtually every programming language and is one of the most powerful tools in a developer's toolkit.
Basic Syntax
Literal Characters
The simplest regex matches exact characters. The pattern hello matches the string "hello" anywhere in the text.
Anchors
| Symbol | Meaning | Example |
|---|---|---|
^ | Start of string | ^hello matches "hello world" but not "say hello" |
$ | End of string | world$ matches "hello world" but not "world peace" |
\b | Word boundary | \bcat\b matches "cat" but not "category" |
Character Classes
| Pattern | Matches |
|---|---|
[abc] | Any one of: a, b, or c |
[a-z] | Any lowercase letter |
[A-Z] | Any uppercase letter |
[0-9] | Any digit |
[^abc] | Any character except a, b, c |
\d | Any digit (same as [0-9]) |
\w | Any word character [a-zA-Z0-9_] |
\s | Any whitespace (space, tab, newline) |
. | Any character except newline |
Quantifiers
| Symbol | Meaning |
|---|---|
* | 0 or more times |
+ | 1 or more times |
? | 0 or 1 time (optional) |
{3} | Exactly 3 times |
{3,} | 3 or more times |
{3,6} | Between 3 and 6 times |
Groups and Capturing
Parentheses create capturing groups that extract specific parts of a match:
Pattern: (d{4})-(d{2})-(d{2})
Input: "Date: 2024-01-15"
Group 1: "2024"
Group 2: "01"
Group 3: "15"Non-capturing Groups
Use (?:...) to group without capturing:
(?:https?|ftp):// // matches http://, https://, or ftp://Common Regex Patterns
| Pattern | Regex |
|---|---|
| Email address | [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} |
| URL | https?://[^\s/$.?#].[^\s]* |
| IPv4 address | (\d{1,3}\.){3}\d{1,3} |
| Phone (US) | \+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} |
| Thai phone | 0[689]\d{8} |
| Hex color | #[0-9a-fA-F]{3,6} |
| Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} |
| Positive integer | ^[1-9]\d*$ |
Flags
| Flag | Meaning |
|---|---|
g | Global — find all matches, not just the first |
i | Case-insensitive matching |
m | Multiline — ^ and $ match start/end of each line |
s | Dot-all — . matches newlines too |
✓ Tip: Test your regex patterns incrementally. Start with the simplest possible pattern and add complexity step by step. Always test with both matching and non-matching examples.