Skip to content

Commit 49960ef

Browse files
Imangazalievziishaned
authored andcommitted
Add tests for regular expressions (ziishaned#20)
1 parent 766f124 commit 49960ef

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

README.md

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ A regular expression is just a pattern of letters and digits that we use to perf
6161
"cat" => The <a href="#learn-regex"><strong>cat</strong></a> sat on the mat
6262
</pre>
6363

64+
[Test the regular expression](https://regex101.com/r/FOq5Nb/1)
65+
6466
The regular expression `123` matches the string "123". The regular expression is matched against an input string by comparing each
6567
character in the regular expression to each character in the input string, one after another. Regular expressions are normally
6668
case-sensitive so the regular expression `Cat` would not match the string "cat".
@@ -69,6 +71,8 @@ case-sensitive so the regular expression `Cat` would not match the string "cat".
6971
"Cat" => The cat sat on the <a href="#learn-regex"><strong>Cat</strong></a>
7072
</pre>
7173

74+
[Test the regular expression](https://regex101.com/r/jw4Vi6/1)
75+
7276
## 2. Meta Characters
7377

7478
Meta characters are the building blocks of the regular expressions. Meta characters do not stand for themselves but instead are
@@ -100,6 +104,8 @@ letter `r`.
100104
".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.
101105
</pre>
102106

107+
[Test the regular expression](https://regex101.com/r/xc9GkU/1)
108+
103109
## 2.2 Character set
104110

105111
Character sets are also called character class. Square brackets are used to specify character sets. Use a hyphen inside a character set to
@@ -110,12 +116,16 @@ expression `[Tt]he` means: an uppercase `T` or lowercase `t`, followed by the le
110116
"[Tt]he" => <a href="#learn-regex"><strong>The</strong></a> car parked in <a href="#learn-regex"><strong>the</strong></a> garage.
111117
</pre>
112118

119+
[Test the regular expression](https://regex101.com/r/2ITLQ4/1)
120+
113121
A period inside a character set, however, means a literal period. The regular expression `ar[.]` means: a lowercase character `a`, followed by letter `r`, followed by a period `.` character.
114122

115123
<pre>
116124
"ar[.]" => A garage is a good place to park a c<a href="#learn-regex"><strong>ar.</strong></a>
117125
</pre>
118126

127+
[Test the regular expression](https://regex101.com/r/wL3xtE/1)
128+
119129
### 2.2.1 Negated character set
120130

121131
In general, the caret symbol represents the start of the string, but when it is typed after the opening square bracket it negates the
@@ -126,6 +136,7 @@ the letter `r`.
126136
"[^c]ar" => The car <a href="#learn-regex"><strong>par</strong></a>ked in the <a href="#learn-regex"><strong>gar</strong></a>age.
127137
</pre>
128138

139+
[Test the regular expression](https://regex101.com/r/nNNlq3/1)
129140

130141
## 2.3 Repetitions
131142

@@ -142,6 +153,8 @@ character set. For example, the regular expression `[a-z]*` means: any number of
142153
"[a-z]*" => T<a href="#learn-regex"><strong>he</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.
143154
</pre>
144155

156+
[Test the regular expression](https://regex101.com/r/7m8me5/1)
157+
145158
The `*` symbol can be used with the meta character `.` to match any string of characters `.*`. The `*` symbol can be used with the
146159
whitespace character `\s` to match a string of whitespace characters. For example, the expression `\s*cat\s*` means: zero or more
147160
spaces, followed by lowercase character `c`, followed by lowercase character `a`, followed by lowercase character `t`, followed by
@@ -151,6 +164,8 @@ zero or more spaces.
151164
"\s*cat\s*" => The fat<a href="#learn-regex"><strong> cat </strong></a>sat on the <a href="#learn-regex">con<strong>cat</strong>enation</a>.
152165
</pre>
153166

167+
[Test the regular expression](https://regex101.com/r/gGrwuz/1)
168+
154169
### 2.3.2 The Plus
155170

156171
The symbol `+` matches one or more repetitions of the preceding character. For example, the regular expression `c.+t` means: lowercase
@@ -160,6 +175,8 @@ letter `c`, followed by any number of character, followed by the lowercase chara
160175
"c.+t" => The fat <a href="#learn-regex"><strong>cat sat on the mat</strong></a>.
161176
</pre>
162177

178+
[Test the regular expression](https://regex101.com/r/Dzf9Aa/1)
179+
163180
### 2.3.3 The Question Mark
164181

165182
In regular expression the meta character `?` makes the preceding character optional. This symbol matches zero or one instance of
@@ -169,10 +186,15 @@ character `h`, followed by the lowercase character `e`.
169186
<pre>
170187
"[T]he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in the garage.
171188
</pre>
189+
190+
[Test the regular expression](https://regex101.com/r/cIg9zm/1)
191+
172192
<pre>
173193
"[T]?he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in t<a href="#learn-regex"><strong>he</strong></a> garage.
174194
</pre>
175195

196+
[Test the regular expression](https://regex101.com/r/kPpO2x/1)
197+
176198
## 2.4 Braces
177199

178200
In regular expression braces that are also called quantifiers are used to specify the number of times that a
@@ -183,17 +205,23 @@ characters in the range of 0 to 9).
183205
"[0-9]{2,3}" => 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.
184206
</pre>
185207

208+
[Test the regular expression](https://regex101.com/r/juM86s/1)
209+
186210
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
187211
the comma the regular expression `[0-9]{2}` means: Match exactly 2 digits.
188212

189213
<pre>
190214
"[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.
191215
</pre>
192216

217+
[Test the regular expression](https://regex101.com/r/Gdy4w5/1)
218+
193219
<pre>
194220
"[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.
195221
</pre>
196222

223+
[Test the regular expression](https://regex101.com/r/gqajq8/1)
224+
197225
## 2.5 Character Group
198226

199227
Character group is a group of sub-pattern that is written inside Parentheses `(...)`. As we discussed before that in regular expression
@@ -206,6 +234,8 @@ We can also use the alternation `|` meta character inside character group. For e
206234
"(c|g|p)ar" => The <a href="#learn-regex"><strong>car</strong></a> is <a href="#learn-regex"><strong>par</strong></a>ked in the <a href="#learn-regex"><strong>gar</strong></a>age.
207235
</pre>
208236

237+
[Test the regular expression](https://regex101.com/r/tUxrBG/1)
238+
209239
## 2.6 Alternation
210240

211241
In regular expression Vertical bar `|` is used to define alternation. Alternation is like a condition between multiple expressions. Now,
@@ -218,6 +248,8 @@ or lowercase character `c`, followed by lowercase character `a`, followed by low
218248
"(T|t)he|car" => <a href="#learn-regex"><strong>The</strong></a> <a href="#learn-regex"><strong>car</strong></a> is parked in <a href="#learn-regex"><strong>the</strong></a> garage.
219249
</pre>
220250

251+
[Test the regular expression](https://regex101.com/r/fBXyX0/1)
252+
221253
## 2.7 Escaping special character
222254

223255
Backslash `\` is used in regular expression to escape the next character. This allows to to specify a symbol as a matching character
@@ -230,6 +262,8 @@ expression `(f|c|m)at\.?` means: lowercase letter `f`, `c` or `m`, followed by l
230262
"(f|c|m)at\.?" => The <a href="#learn-regex"><strong>fat</strong></a> <a href="#learn-regex"><strong>cat</strong></a> sat on the <a href="#learn-regex"><strong>mat.</strong></a>
231263
</pre>
232264

265+
[Test the regular expression](https://regex101.com/r/DOc5Nu/1)
266+
233267
## 2.8 Anchors
234268

235269
In regular expressions, to check if the matching symbol is the starting symbol or ending symbol of the input string for this purpose
@@ -248,10 +282,14 @@ followed by lowercase character `h`, followed by lowercase character `e`.
248282
"(T|t)he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in <a href="#learn-regex"><strong>the</strong></a> garage.
249283
</pre>
250284

285+
[Test the regular expression](https://regex101.com/r/5ljjgB/1)
286+
251287
<pre>
252288
"^(T|t)he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in the garage.
253289
</pre>
254290

291+
[Test the regular expression](https://regex101.com/r/jXrKne/1)
292+
255293
### 2.8.2 Dollar
256294

257295
Dollar `$` symbol is used to check if matching character is the last character of the input string. For example, regular expression
@@ -262,10 +300,14 @@ must be end of the string.
262300
"(at\.)" => The fat c<a href="#learn-regex"><strong>at.</strong></a> s<a href="#learn-regex"><strong>at.</strong></a> on the m<a href="#learn-regex"><strong>at.</strong></a>
263301
</pre>
264302

303+
[Test the regular expression](https://regex101.com/r/y4Au4D/1)
304+
265305
<pre>
266-
"(at\.)$" => The fat cat sat on the m<a href="#learn-regex"><strong>at.</strong></a>
306+
"(at\.)$" => The fat cat. sat. on the m<a href="#learn-regex"><strong>at.</strong></a>
267307
</pre>
268308

309+
[Test the regular expression](https://regex101.com/r/t0AkOd/1)
310+
269311
## 3. Shorthand Character Sets
270312

271313
Regular expression provides shorthands for the commonly used character sets, which offer convenient shorthands for commonly used
@@ -309,6 +351,8 @@ followed by letter `h`, followed by letter `e`. In braces we define positive loo
309351
"(T|t)he(?=\sfat)" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on the mat.
310352
</pre>
311353

354+
[Test the regular expression](https://regex101.com/r/apqJZq/1)
355+
312356
### 4.2 Negative Lookahead
313357

314358
Negative lookahead is used when we need to get all matches from input string that are not followed by a pattern. Negative lookahead
@@ -320,6 +364,8 @@ input string that are not followed by the word `fat` precedes by a space charact
320364
"(T|t)he(?!\sfat)" => The fat cat sat on <a href="#learn-regex"><strong>the</strong></a> mat.
321365
</pre>
322366

367+
[Test the regular expression](https://regex101.com/r/sswCvQ/1)
368+
323369
### 4.3 Positive Lookbehind
324370

325371
Positive lookbehind is used to get all the matches that are preceded by a specific pattern. Positive lookbehind is denoted by
@@ -330,6 +376,8 @@ are after the word `The` or `the`.
330376
"(?<=(T|t)he\s)(fat|mat)" => The <a href="#learn-regex"><strong>fat</strong></a> cat sat on the <a href="#learn-regex"><strong>mat</strong></a>.
331377
</pre>
332378

379+
[Test the regular expression](https://regex101.com/r/TZ8DOX/1/)
380+
333381
### 4.4 Negative Lookbehind
334382

335383
Negative lookbehind is used to get all the matches that are not preceded by a specific pattern. Negative lookbehind is denoted by
@@ -340,6 +388,8 @@ are not after the word `The` or `the`.
340388
"(?&lt;!(T|t)he\s)(cat)" => The cat sat on <a href="#learn-regex"><strong>cat</strong></a>.
341389
</pre>
342390

391+
[Test the regular expression](https://regex101.com/r/gleYg9/1)
392+
343393
## 5. Flags
344394

345395
Flags are also called modifiers because they modify the output of a regular expression. These flags can be used in any order or
@@ -362,28 +412,33 @@ the whole input string.
362412
"The" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on the mat.
363413
</pre>
364414

415+
[Test the regular expression](https://regex101.com/r/dpQyf9/1)
416+
365417
<pre>
366418
"/The/gi" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on <a href="#learn-regex"><strong>the</strong></a> mat.
367419
</pre>
368420

369-
### 5.2 Global search
421+
[Test the regular expression](https://regex101.com/r/ahfiuh/1)
370422

423+
### 5.2 Global search
371424

372425
The `g` modifier is used to perform a global match (find all matches rather than stopping after the first match). For example, the
373426
regular expression`/.(at)/g` means: any character except new line, followed by lowercase character `a`, followed by lowercase
374427
character `t`. Because we provided `g` flag at the end of the regular expression now it will find every matches from whole input
375428
string.
376429

377-
378-
379430
<pre>
380-
".(at)" => The <a href="#learn-regex"><strong>fat</strong></a> cat sat on the mat.
431+
"/.(at)/" => The <a href="#learn-regex"><strong>fat</strong></a> cat sat on the mat.
381432
</pre>
382433

434+
[Test the regular expression](https://regex101.com/r/jnk6gM/1)
435+
383436
<pre>
384437
"/.(at)/g" => The <a href="#learn-regex"><strong>fat</strong></a> <a href="#learn-regex"><strong>cat</strong></a> <a href="#learn-regex"><strong>sat</strong></a> on the <a href="#learn-regex"><strong>mat</strong></a>.
385438
</pre>
386439

440+
[Test the regular expression](https://regex101.com/r/dO1nef/1)
441+
387442
### 5.3 Multiline
388443

389444
The `m` modifier is used to perform a multi-line match. As we discussed earlier anchors `(^, $)` are used to check if pattern is
@@ -397,12 +452,16 @@ line. And because of `m` flag now regular expression engine matches pattern at t
397452
on the <a href="#learn-regex"><strong>mat.</strong></a>
398453
</pre>
399454

455+
[Test the regular expression](https://regex101.com/r/hoGMkP/1)
456+
400457
<pre>
401458
"/.at(.)?$/gm" => The <a href="#learn-regex"><strong>fat</strong></a>
402459
cat <a href="#learn-regex"><strong>sat</strong></a>
403460
on the <a href="#learn-regex"><strong>mat.</strong></a>
404461
</pre>
405462

463+
[Test the regular expression](https://regex101.com/r/E88WE2/1)
464+
406465
## Bonus
407466

408467
* *Positive Integers*: `^\d+$`

0 commit comments

Comments
 (0)