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
Controls
Match Details
a digit (0-9) (one or more times).
Match List
1234index 7-102026index 26-2903index 31-3231index 34-3556index 53-5478index 56-573index 63Data Table
(0 rows)| # | Pattern | Flags | Test String | Matches | Groups |
|---|
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.
| Pattern | Matches |
|---|---|
| [abc] | a, b, or c |
| [a-z] | Any lowercase letter |
| [^abc] | Any character except a, b, or c |
| \d | Any digit (same as [0-9]) |
| \w | Word character (letter, digit, underscore) |
| \s | Whitespace (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.
| Pattern | Meaning |
|---|---|
| * | 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) |
| \b | Word 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).
| Pattern | Purpose |
|---|---|
| (abc) | Capture group (stores matched text) |
| (?:abc) | Non-capturing group |
| a|b | Match a or b (alternation) |
| (?=abc) | Positive lookahead |
| (?!abc) | Negative lookahead |
| \1 | Backreference 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 Case | Pattern |
|---|---|
| Integer | -?\d+ |
| Decimal | -?\d+\.?\d* |
| [a-zA-Z0-9._%+-]+@[\w.-]+\.\w{2,} | |
| URL | https?://[\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.