0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>In this lesson, we will look at quantification and observe how to find repeating characters using it.</p>
1
<p>In this lesson, we will look at quantification and observe how to find repeating characters using it.</p>
2
<p><strong>Quantification</strong>is searching for sequences. A<strong>quantifier</strong>is a notation that specifies the number of possible repetitions of a character, a group of characters, or a character class in a regular expression before it.</p>
2
<p><strong>Quantification</strong>is searching for sequences. A<strong>quantifier</strong>is a notation that specifies the number of possible repetitions of a character, a group of characters, or a character class in a regular expression before it.</p>
3
<p>We will see what all this means. Look at an example with the simplest quantifier ?, which means to search for matches repeating from zero to one time:</p>
3
<p>We will see what all this means. Look at an example with the simplest quantifier ?, which means to search for matches repeating from zero to one time:</p>
4
<p>/colou?/</p>
4
<p>/colou?/</p>
5
<p>colr, color, colour, colouur, colouuur</p>
5
<p>colr, color, colour, colouur, colouuur</p>
6
<p>There is no grouping or character class in this expression. So, the quantifier ? specifies the number of repetitions for the character u. It means the preceding character u will repeat ether:</p>
6
<p>There is no grouping or character class in this expression. So, the quantifier ? specifies the number of repetitions for the character u. It means the preceding character u will repeat ether:</p>
7
<ul><li>Zero times - it does not repeat at all</li>
7
<ul><li>Zero times - it does not repeat at all</li>
8
<li>One time - it occurs once without repetitions</li>
8
<li>One time - it occurs once without repetitions</li>
9
</ul><p>The result is four matches.</p>
9
</ul><p>The result is four matches.</p>
10
<p>And in this example, we will add the character r to the pattern string. It will only give us two matches:</p>
10
<p>And in this example, we will add the character r to the pattern string. It will only give us two matches:</p>
11
<p>/colou?r/</p>
11
<p>/colou?r/</p>
12
<p>colr, color, colour, colouur, colouuur</p>
12
<p>colr, color, colour, colouur, colouuur</p>
13
<p>Using a grouping and a character class, we get different matches:</p>
13
<p>Using a grouping and a character class, we get different matches:</p>
14
<ul><li>With grouping, we check for the occurrence of the whole group for zero or one time</li>
14
<ul><li>With grouping, we check for the occurrence of the whole group for zero or one time</li>
15
<li>With character classes, we check if one of the characters enters for zero or one time. For this case, we don't examine all characters simultaneously</li>
15
<li>With character classes, we check if one of the characters enters for zero or one time. For this case, we don't examine all characters simultaneously</li>
16
</ul><p>/col(ou)?r/</p>
16
</ul><p>/col(ou)?r/</p>
17
<p>colr, color, colour, colouur, colouuur</p>
17
<p>colr, color, colour, colouur, colouuur</p>
18
<p>/col[ou]?r/</p>
18
<p>/col[ou]?r/</p>
19
<p>colr, color, colour, colouur, colouuur</p>
19
<p>colr, color, colour, colouur, colouuur</p>
20
<p>Another often-used quantifier is the + character. It indicates that we must find the preceding character, group, or class of characters at least once. It is what happens. Here, the word<em>color</em>is no longer matched:</p>
20
<p>Another often-used quantifier is the + character. It indicates that we must find the preceding character, group, or class of characters at least once. It is what happens. Here, the word<em>color</em>is no longer matched:</p>
21
<p>/colou+r/</p>
21
<p>/colou+r/</p>
22
<p>colr, color, colour, colouur, colouuur</p>
22
<p>colr, color, colour, colouur, colouuur</p>
23
<p>The character * indicates either no repetition or multiple repetitions, giving us a match in all substrings:</p>
23
<p>The character * indicates either no repetition or multiple repetitions, giving us a match in all substrings:</p>
24
<p>/colou*r/</p>
24
<p>/colou*r/</p>
25
<p>colr, color, colour, colouur, colouuur</p>
25
<p>colr, color, colour, colouur, colouuur</p>
26
<p>There are also more specific quantifiers - we write them in curly brackets {}. Between them, you enter the number of repetitions you need:</p>
26
<p>There are also more specific quantifiers - we write them in curly brackets {}. Between them, you enter the number of repetitions you need:</p>
27
<p>/colou{2}r/</p>
27
<p>/colou{2}r/</p>
28
<p>colr, color, colour, colouur, colouuur</p>
28
<p>colr, color, colour, colouur, colouuur</p>
29
<p>In addition, you can enter a range of repetitions in curly brackets {}. For example, from two to three:</p>
29
<p>In addition, you can enter a range of repetitions in curly brackets {}. For example, from two to three:</p>
30
<p>/colou{2,3}r/</p>
30
<p>/colou{2,3}r/</p>
31
<p>colr, color, colour, colouur, colouuur</p>
31
<p>colr, color, colour, colouur, colouuur</p>
32
<p>If we don't enter a number for the upper bound of the range, there won't be a maximum number of repetitions:</p>
32
<p>If we don't enter a number for the upper bound of the range, there won't be a maximum number of repetitions:</p>
33
<p>/colou{1,}r/</p>
33
<p>/colou{1,}r/</p>
34
<p>colr, color, colour, colouur, colouuur, colouuuur, colouuuuur</p>
34
<p>colr, color, colour, colouur, colouuur, colouuuur, colouuuuur</p>
35
35