03. Repetitions and Optionals

Reiterating a single regex expression

If we want to reiterate a single regex expression, we can do so with braces ({}). Within the braces we can specify up to two numbers, separated by a comma.

Match preceding element (if it occurs) exactly n times.
at least n times, but no more than m times.
n or more times.
no more than m times.

Here are some concrete examples:

Match any character 4 or more times.
Match any digit between 5 and 10 times.
Matching zero or many elements

To match an element zero or many times, use the Kleene star, which is an asterisk (*) placed after a specific character.

Match at least one element

To match a previous element at least once, use the Kleene plus (+). This is a tighter regulation than the Kleene star, as it ensures there is at least one of the preceding character.


To match an element zero or just one time, use the question mark (?). To match an actual question mark, escape it. (\?).

Match zero or many digits.
Match one or many lowercase alphabet letters.
Match the characters a, b or c zero or just once.


You can use conditionals to specify either-or relations. Use the logical or (|) between two options.

Selects either cats or dogs.

Build modern and responsive webpages

The Modern Web

Build modern and responsive webpages Try Front-end

The Modern Web will guide you through the most important tools of device-agnostic web development, including HTML5, CSS3, and JavaScript. The plain-english explanations and practical examples emphasize the techniques, principles, and practices that you'll need to easily transcend individual browser quirks and stay relevant as these technologies are updated.

$ Check price
34.9534.95Amazon 4.5 logo(30+ reviews)

More Front-end resources

Learn how data is stored

Data Structures and Algorithms Made Easy

Learn how data is stored Try Data Structures

A handy guide of sorts for any computer science professional, Data Structures And Algorithms Made Easy in Java: Data Structure And Algorithmic Puzzles is a solution bank for various complex problems related to data structures and algorithms. It can be used as a reference manual by those readers in the computer science industry.

$ Check price
39.9939.99Amazon 4.5 logo(339+ reviews)

More Data Structures resources