All Labs

Regex & Pattern Matching Lab

Build regular expressions and see matches highlighted in real time. Experiment with character classes, quantifiers, anchors, groups, and flags to master the language of pattern matching.

Guided Experiment: Character Classes and Quantifiers

How do character classes ([a-z], \d, \w) and quantifiers (*, +, ?, {n,m}) work together to define what a regex matches? What happens when you change a quantifier from + to *?

Write your hypothesis in the Lab Report panel, then click Next.

Match Visualization

Order #1234 was placed on 2026-03-31. The total was $56.78 for 3 items.
Full match

Controls

//g

Match Details

7
Matches
No
Full Match
Pattern Explanation

a digit (0-9) (one or more times).

Match List

11234index 7-10
22026index 26-29
303index 31-32
431index 34-35
556index 53-54
678index 56-57
73index 63

Data Table

(0 rows)
#PatternFlagsTest StringMatchesGroups
0 / 500
0 / 500
0 / 500

Reference Guide

Character Classes

Character classes match one character from a defined set. Square brackets define a custom class, while shorthand codes match common categories.

PatternMatches
[abc]a, b, or c
[a-z]Any lowercase letter
[^abc]Any character except a, b, or c
\dAny digit (same as [0-9])
\wWord character (letter, digit, underscore)
\sWhitespace (space, tab, newline)
.Any character except newline

Uppercase versions (\D, \W, \S) match the opposite of their lowercase counterparts.

Quantifiers & Anchors

Quantifiers control how many times the preceding element can repeat. Anchors match a position in the string rather than a character.

PatternMeaning
*Zero or more times
+One or more times
?Zero or one time (optional)
{n}Exactly n times
{n,m}Between n and m times
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary

Add ? after a quantifier to make it lazy (match as few as possible instead of as many).

Groups & Alternation

Parentheses create groups that capture matched text and allow you to apply quantifiers to sequences. The pipe character provides alternation (logical OR).

PatternPurpose
(abc)Capture group (stores matched text)
(?:abc)Non-capturing group
a|bMatch a or b (alternation)
(?=abc)Positive lookahead
(?!abc)Negative lookahead
\1Backreference to group 1

Capture groups are numbered left to right by their opening parenthesis. Use (?:) when you need grouping without capturing.

Common Patterns

These frequently used patterns demonstrate how regex components combine to match real-world text formats.

Use CasePattern
Integer-?\d+
Decimal-?\d+\.?\d*
Email[a-zA-Z0-9._%+-]+@[\w.-]+\.\w{2,}
URLhttps?://[\w.-]+(/[\w./?#&=-]*)?
Date\d{4}-\d{2}-\d{2}
Hex Color#[0-9a-fA-F]{3,6}

These patterns work for basic validation. Production applications often need more specific patterns to handle edge cases and conform to formal specifications.