Repeat Pattern Matching
As the name implies repeat pattern matching is used to test for the repeated occurrence of a sequence of characters. A shorthand notation is provided for commonly used repeat patterns.
| Symbol | Meaning | Example |
| {n} | Match exactly n repetitions | \d{3} matches "999" but not "99" |
| {n,} | Match n or more occurrence of the character | Size X{1,}L matches "Size XL" and "Size XXL" but not "Size L" |
| {n,m} | Match between n and m occurrence of the character | Size X{1,2}L matches "Size XL" and "Size XXL" but not "Size XXXL" |
| ? | Match 0 or 1 occurrence of the character - equivalent to {0,1} | Size X?L matches "Size L" and "Size XL" but not "Size XXL" |
| * | Match 0 or more occurrences of the character - equivalent to {0,} | Size X*L matches "Size L" and "Size XL"and"Size XXL" |
| + | Match 1 or more occurrences of the character - equivalent to {1,} | Size X+L matches "Size XL" and "Size XXL" but not "Size L" |
The * and + specifiers have a problem - they are greedy, i.e. they stop matching only when they fail to find a match which can result in overmatching. This can be prevented by instructing these specifiers to be lazy
as discussed here.
Regular Expression Sandbox
Model
Data
Do not wrap the model expression in a /.../ pair. The characters
Do not wrap the model expression in a /.../ pair. The characters
^$.?*!+:=()[]{}|\\ must be escaped - except when then occur inside a character class. Invalid characters will be grayed out.| Result | Left Text | Match | Right Text |
|---|---|---|---|
Download