Master Regular Expressions

A structured guide to syntax and patterns.

1

The Basics

Regular expressions match characters literally unless they are special meta-characters.

helloExample

Matches the exact string 'hello'. Case sensitive by default.

Matchhello world
123Example

Matches the exact digits '123'.

MatchOrder 123
2

Character Classes

Define a set of characters to match using special codes.

\dExample

Matches any single digit (0-9).

MatchPage 5
\wExample

Matches any 'word' character (letters, numbers, underscore).

Matchuser_1
\sExample

Matches any whitespace (space, tab, newline).

Matchhello world
.Example

The wild card. Matches ANY character except newline.

Matcha.c matches abc, adc
3

Sets and Ranges

Create custom lists of characters to match.

[abc]Example

Matches either 'a', 'b', or 'c'.

Matchapple
[a-z]Example

Matches any lowercase letter.

Matchapple
[^0-9]Example

The ^ inside brackets negates the set. Matches anything NOT a digit.

MatchText only
4

Quantifiers

Specify how many times the previous character/group should repeat.

a+Example

Matches one or more 'a's.

Matchaardvark
a*Example

Matches zero or more 'a's.

Matchbaaa
colou?rExample

The '?' makes the 'u' optional. Matches color and colour.

Matchcolor
\d{3}Example

Matches exactly 3 digits.

Match555
5

Anchors

Lock the pattern to specific positions in the text.

^StartExample

Matches 'Start' only at the beginning of the string/line.

MatchStart here
End$Example

Matches 'End' only at the end of the string/line.

MatchThe End
\bword\bExample

Boundary. Ensures 'word' is a whole word, not part of 'sword'.

Matchword
6

Lookaround Assertions

Check for patterns without including them in the match. Zero-width assertions that look ahead or behind.

\w+(?=\s+is\b)Example

Positive Lookahead: Matches words that are followed by ' is'.

MatchThe cat is happy, the dog is brown
\b\d{3}(?!-\d{4})Example

Negative Lookahead: Matches 3 digits NOT followed by '-4 digits'.

MatchPhone: 123-4567, Code: 789
(?<=\$)\d+Example

Positive Lookbehind: Matches digits preceded by a dollar sign.

MatchPrice: $99, Total: $150
\b\w+\b(?<!\bthe\b)Example

Negative Lookbehind: Matches words NOT preceded by 'the'.

Matchthe cat and dog play
7

Named Capture Groups

Give your capture groups meaningful names instead of relying on numeric indices. Makes patterns more readable and maintainable.

(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})Example

Named groups for date parsing. Access captures by name instead of index.

MatchDate: 2025-12-15
(?<protocol>https?):\/\/(?<domain>[^/]+)(?<path>\/[^\s]*)?Example

Named groups for URL parsing: protocol, domain, and path.

MatchVisit: https://example.com/path/to/page
(?<area>\d{3})-(?<exchange>\d{3})-(?<number>\d{4})Example

Named groups for phone number components.

MatchCall: 555-123-4567
(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})Example

Named groups for time components.

MatchTime: 14:30:25
8

Unicode Properties

Match characters by their Unicode properties. Works with international text, emojis, and special characters. Requires the 'u' flag.

\p{L}+Example

Unicode Letters: matches letters from any language including accented characters and non-Latin scripts.

MatchHello 你好 مرحبا Здравствуйте
\p{N}+Example

Unicode Numbers: matches numbers including Roman numerals, fractions, and numbers from other scripts.

Match123 456 Ⅷ ⑨
[\p{Emoji}]Example

Emoji Characters: matches any emoji character (if supported by engine).

Match🌍 ⭐ 🎉 💡
[^\p{L}\p{N}\s]+Example

Non-alphanumeric symbols: matches symbols, punctuation, and special Unicode characters.

Match@#$% ★♥♦ †‡

Ready to practice?

Take what you've learned and test it in the real-time editor.

Go to Tester