VOOZH about

URL: https://regexone.com/lesson/repeating_characters

⇱ RegexOne - Learn Regular Expressions - Lesson 6: Catching some zzz's


👁 RegexOne icon
RegexOne
Learn Regular Expressions with simple, interactive exercises.
👁 Interactive Tutorial
Interactive Tutorial 👁 References & More
References & More
All Lessons
Lesson 1: An Introduction, and the ABCs Lesson 1½: The 123s Lesson 2: The Dot Lesson 3: Matching specific characters Lesson 4: Excluding specific characters Lesson 5: Character ranges Lesson 6: Catching some zzz's Lesson 7: Mr. Kleene, Mr. Kleene Lesson 8: Characters optional Lesson 9: All this whitespace Lesson 10: Starting and ending Lesson 11: Match groups Lesson 12: Nested groups Lesson 13: More group work Lesson 14: It's all conditional Lesson 15: Other special characters Lesson X: Infinity and beyond!
Practice Problems
Problem 1: Matching a decimal numbers Problem 2: Matching phone numbers Problem 3: Matching emails Problem 4: Matching HTML Problem 5: Matching specific filenames Problem 6: Trimming whitespace from start and end of line Problem 7: Extracting information from a log file Problem 8: Parsing and extracting data from a URL Problem X: Infinity and beyond!
Language Guides
C# Javascript Java PHP Python
Lesson 6: Catching some zzz's

Note: Some parts of the repetition syntax below isn't supported in all regular expression implementations.

We've so far learned how to specify the range of characters we want to match, but how about the number of repetitions of characters that we want to match? One way that we can do this is to explicitly spell out exactly how many characters we want, eg. \d\d\d which would match exactly three digits.

A more convenient way is to specify how many repetitions of each character we want using the curly braces notation. For example, a{3} will match the a character exactly three times. Certain regular expression engines will even allow you to specify a range for this repetition such that a{1,3} will match the a character no more than 3 times, but no less than once for example.

This quantifier can be used with any character, or special metacharacters, for example w{3} (three w's), [wxy]{5} (five characters, each of which can be a w, x, or y) and .{2,6} (between two and six of any character).

In the lines below, the last string with only one z isn't what we would consider a proper spelling of the slang "wazzup?". Try writing a pattern that matches only the first two spellings by using the curly brace notation above.

Exercise 6: Matching repeated characters
Task Text
match wazzzzzup 👁 To be completed
match wazzzup 👁 To be completed
skip wazup 👁 To be completed
Solution

There are a couple 'z's in the first two lines we have to match, so the expression waz{3,5}up will match all strings with that many 'z's.

Solve the above task to continue on to the next problem, or read the Solution.
Find RegexOne useful? Please consider
Donating ($4) via Paypal to support our site.
Lesson Notes
abc… Letters
123… Digits
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(abc|def) Matches abc or def
2025 © RegexOne
Email | Twitter
Additional Courses
Interactive SQL Lessons