Dev / IT9 min read

Regular Expressions (Regex) Guide for Developers

A practical regex guide with real examples. Learn anchors, character classes, quantifiers, groups, and lookaheads — with patterns you can use today.

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

SymbolMeaningExample
^Start of string^hello matches "hello world" but not "say hello"
$End of stringworld$ matches "hello world" but not "world peace"
\bWord boundary\bcat\b matches "cat" but not "category"

Character Classes

PatternMatches
[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
\dAny digit (same as [0-9])
\wAny word character [a-zA-Z0-9_]
\sAny whitespace (space, tab, newline)
.Any character except newline

Quantifiers

SymbolMeaning
*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

PatternRegex
Email address[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
URLhttps?://[^\s/$.?#].[^\s]*
IPv4 address(\d{1,3}\.){3}\d{1,3}
Phone (US)\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Thai phone0[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

FlagMeaning
gGlobal — find all matches, not just the first
iCase-insensitive matching
mMultiline — ^ and $ match start/end of each line
sDot-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.

TRY THE FREE TOOL

Regex Tester

Test regular expressions with live matching

Open Tool →
← Back to all articles