Skip to content

Commit 05a7e90

Browse files
committed
Add quantifiers
1 parent 6ac2788 commit 05a7e90

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ A regular expression is just a pattern of letters and digits that we used to sea
3535
`cat` means: the letter `c`, followed by the letter `a`, followed by the letter `t`.
3636

3737
<pre>
38-
"cat" => The <strong><a href="#learn-regex">cat</a></strong> sat on the mat
38+
"cat" => The <a href="#learn-regex"><strong>cat</strong></a> sat on the mat
3939
</pre>
4040

4141
The regular expression `123` matches the string "123". The regular expression is matched against an input string by comparing each
4242
character in the regular expression to each character in the input string, one after another. Regular expressions are normally
4343
case-sensitive so the regular expression `Cat` would not match the string "cat".
4444

4545
<pre>
46-
"Cat" => The cat sat on the <strong><a href="#learn-regex">Cat</a></strong>
46+
"Cat" => The cat sat on the <a href="#learn-regex"><strong>Cat</strong></a>
4747
</pre>
4848

4949
## 2. Meta Characters
@@ -60,7 +60,7 @@ The meta character are as follows:
6060
|*|Matches 0 or more repetitions of the preceding symbol.|
6161
|+|Matches 1 or more repetitions of the preceding symbol.
6262
|?|Makes the preceding symbol optional.|
63-
|{n}|Braces. Matches “n” repetitions of the preceding symbol.|
63+
|{n,m}|Braces. Matches at least "n" but not more than "m" repetitions of the preceding symbol.|
6464
|(xyz)|Character group. Matches the characters xyz in that exact order.|
6565
|&#124;|Alternation. Matches either the characters before or the characters after the symbol.|
6666
|&#92;|Escapes the next character. This allows you to match reserved characters <code>[ ] ( ) { } . * + ? ^ $ \ &#124;</code>|
@@ -74,7 +74,7 @@ or new line characters. For example the regular expression `.ar` means: any char
7474
letter `r`.
7575

7676
<pre>
77-
".ar" => The <strong><a href="#learn-regex">car</a></strong> <strong><a href="#learn-regex">par</a></strong>ked in the <strong><a href="#learn-regex">gar</a></strong>age.
77+
".ar" => The <a href="#learn-regex"><strong>car</strong></a> <a href="#learn-regex"><strong>par</strong></a>ked in the <a href="#learn-regex"><strong>gar</strong></a>age.
7878
</pre>
7979

8080
## 2.2 Character set
@@ -84,13 +84,13 @@ specify the characters range. The order of the character range inside square bra
8484
expression `[Tt]he` means: an uppercase `T` or lowercase `t`, followed by the letter `h`, followed by the letter `e`.
8585

8686
<pre>
87-
"[Tt]he" => <strong><a href="#learn-regex">The</a></strong> car parked in <strong><a href="#learn-regex">the</a></strong> garage.
87+
"[Tt]he" => <a href="#learn-regex"><strong>The</strong></a> car parked in <a href="#learn-regex"><strong>the</strong></a> garage.
8888
</pre>
8989

9090
Just like above example the regular expression `ar[.]` means: an lowercase character `a`, followed by letter `r`, followed by any character.
9191

9292
<pre>
93-
"ar[.]" => The car p<strong><a href="#learn-regex">ark</a></strong>ed in the g<strong><a href="#learn-regex">ara</a></strong>ge.
93+
"ar[.]" => The car p<a href="#learn-regex"><strong>ark</strong></a>ed in the g<a href="#learn-regex"><strong>ara</strong></a>ge.
9494
</pre>
9595

9696
### 2.2.1 Negated character set
@@ -100,7 +100,7 @@ character set. For example the regular expression `[^c]ar` means: any character
100100
the letter `r`.
101101

102102
<pre>
103-
"[^c]ar" => The car <strong><a href="#learn-regex">par</a></strong>ked in the <strong><a href="#learn-regex">gar</a></strong>age.
103+
"[^c]ar" => The car <a href="#learn-regex"><strong>par</strong></a>ked in the <a href="#learn-regex"><strong>gar</strong></a>age.
104104
</pre>
105105

106106

@@ -116,7 +116,7 @@ of preceding lowercase character `a`. But if it apperas after a character set or
116116
character set. For example the regular expression `[a-z]*` means: any number of lowercase letters in a row.
117117

118118
<pre>
119-
"[a-z]*" => <strong><a href="#learn-regex">The</a></strong> <strong><a href="#learn-regex">car</a></strong> <strong><a href="#learn-regex">parked</a></strong> <strong><a href="#learn-regex">in</a></strong> <strong><a href="#learn-regex">the</a></strong> <strong><a href="#learn-regex">garage</a></strong> #21.
119+
"[a-z]*" => <a href="#learn-regex"><strong>The</strong></a> <a href="#learn-regex"><strong>car</strong></a> <a href="#learn-regex"><strong>parked</strong></a> <a href="#learn-regex"><strong>in</strong></a> <a href="#learn-regex"><strong>the</strong></a> <a href="#learn-regex"><strong>garage</strong></a> #21.
120120
</pre>
121121

122122
The `*` symbol can be used with the meta character `.` to match any string of characters `.*`. The `*` symbol can be used with the
@@ -125,7 +125,7 @@ spaces, followed by lowercase character `c`, followed by lowercase character `a`
125125
zero or more spaces.
126126

127127
<pre>
128-
"\s*cat\s*" => The fat<strong><a href="#learn-regex"> cat </a></strong>sat on the <strong><a href="#learn-regex">cat</a></strong>.
128+
"\s*cat\s*" => The fat<a href="#learn-regex"><strong> cat </strong></a>sat on the <a href="#learn-regex"><strong>cat</strong></a>.
129129
</pre>
130130

131131
### 2.3.2 The Plus
@@ -134,7 +134,7 @@ The symbol `+` matches one or more repetitions of the preceding character. For e
134134
letter `c`, followed by any number of character, followed by the lowercase character `t`.
135135

136136
<pre>
137-
"c.+t" => The fat <strong><a href="#learn-regex">cat sat on the mat</a></strong>.
137+
"c.+t" => The fat <a href="#learn-regex"><strong>cat sat on the mat</strong></a>.
138138
</pre>
139139

140140
### 2.3.3 The Question Mark
@@ -144,9 +144,29 @@ the preceding character. For example the regular expression `[T]?he` means: Opti
144144
character `h`, followed by the lowercase character `e`.
145145

146146
<pre>
147-
"[T]he" => <strong><a href="#learn-regex">The</a></strong> car is parked in the garage.
147+
"[T]he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in the garage.
148148
</pre>
149149
<pre>
150-
"[T]?he" => <strong><a href="#learn-regex">The</a></strong> car is parked in t<strong><a href="#learn-regex">he</a></strong> garage.
150+
"[T]?he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in t<a href="#learn-regex"><strong>he</strong></a> garage.
151151
</pre>
152152

153+
## 2.4 Braces
154+
155+
In regular expression braces that are also called quantifiers used to specify the number of times that a group of character or a
156+
character can be repeated. For example the regular expression `[0-9]{2,3}` means: Match at least 2 digits but not more than 3 (
157+
characters in the range of 0 to 9).
158+
159+
<pre>
160+
"[0-9]{2}" => The number was 9.<a href="#learn-regex"><strong>999</strong></a>7 but we rounded it off to <a href="#learn-regex"><strong>10</strong></a>.0.
161+
</pre>
162+
163+
We can leave out the second number. For example the regular expression `[0-9]{2,}` means: Match 2 or more digits. If we also remove
164+
the comma the regular expression `[0-9]{2}` means: Match exactly 2 digits.
165+
166+
<pre>
167+
"[0-9]{2,}" => The number was 9.<a href="#learn-regex"><strong>9997</strong></a> but we rounded it off to <a href="#learn-regex"><strong>10</strong></a>.0.
168+
</pre>
169+
170+
<pre>
171+
"[0-9]{2}" => The number was 9.<a href="#learn-regex"><strong>99</strong></a><a href="#learn-regex"><strong>97</strong></a> but we rounded it off to <a href="#learn-regex"><strong>10</strong></a>.0.
172+
</pre>

0 commit comments

Comments
 (0)