You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+64-5Lines changed: 64 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,8 @@ A regular expression is just a pattern of letters and digits that we use to perf
61
61
"cat" => The <ahref="#learn-regex"><strong>cat</strong></a> sat on the mat
62
62
</pre>
63
63
64
+
[Test the regular expression](https://regex101.com/r/FOq5Nb/1)
65
+
64
66
The regular expression `123` matches the string "123". The regular expression is matched against an input string by comparing each
65
67
character in the regular expression to each character in the input string, one after another. Regular expressions are normally
66
68
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".
69
71
"Cat" => The cat sat on the <ahref="#learn-regex"><strong>Cat</strong></a>
70
72
</pre>
71
73
74
+
[Test the regular expression](https://regex101.com/r/jw4Vi6/1)
75
+
72
76
## 2. Meta Characters
73
77
74
78
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`.
100
104
".ar" => The <ahref="#learn-regex"><strong>car</strong></a> <ahref="#learn-regex"><strong>par</strong></a>ked in the <ahref="#learn-regex"><strong>gar</strong></a>age.
101
105
</pre>
102
106
107
+
[Test the regular expression](https://regex101.com/r/xc9GkU/1)
108
+
103
109
## 2.2 Character set
104
110
105
111
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
110
116
"[Tt]he" => <ahref="#learn-regex"><strong>The</strong></a> car parked in <ahref="#learn-regex"><strong>the</strong></a> garage.
111
117
</pre>
112
118
119
+
[Test the regular expression](https://regex101.com/r/2ITLQ4/1)
120
+
113
121
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.
114
122
115
123
<pre>
116
124
"ar[.]" => A garage is a good place to park a c<ahref="#learn-regex"><strong>ar.</strong></a>
117
125
</pre>
118
126
127
+
[Test the regular expression](https://regex101.com/r/wL3xtE/1)
128
+
119
129
### 2.2.1 Negated character set
120
130
121
131
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`.
126
136
"[^c]ar" => The car <ahref="#learn-regex"><strong>par</strong></a>ked in the <ahref="#learn-regex"><strong>gar</strong></a>age.
127
137
</pre>
128
138
139
+
[Test the regular expression](https://regex101.com/r/nNNlq3/1)
129
140
130
141
## 2.3 Repetitions
131
142
@@ -142,6 +153,8 @@ character set. For example, the regular expression `[a-z]*` means: any number of
[Test the regular expression](https://regex101.com/r/7m8me5/1)
157
+
145
158
The `*` symbol can be used with the meta character `.` to match any string of characters `.*`. The `*` symbol can be used with the
146
159
whitespace character `\s` to match a string of whitespace characters. For example, the expression `\s*cat\s*` means: zero or more
147
160
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.
151
164
"\s*cat\s*" => The fat<ahref="#learn-regex"><strong> cat </strong></a>sat on the <ahref="#learn-regex">con<strong>cat</strong>enation</a>.
152
165
</pre>
153
166
167
+
[Test the regular expression](https://regex101.com/r/gGrwuz/1)
168
+
154
169
### 2.3.2 The Plus
155
170
156
171
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
160
175
"c.+t" => The fat <ahref="#learn-regex"><strong>cat sat on the mat</strong></a>.
161
176
</pre>
162
177
178
+
[Test the regular expression](https://regex101.com/r/Dzf9Aa/1)
179
+
163
180
### 2.3.3 The Question Mark
164
181
165
182
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`.
169
186
<pre>
170
187
"[T]he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in the garage.
171
188
</pre>
189
+
190
+
[Test the regular expression](https://regex101.com/r/cIg9zm/1)
191
+
172
192
<pre>
173
193
"[T]?he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in t<ahref="#learn-regex"><strong>he</strong></a> garage.
174
194
</pre>
175
195
196
+
[Test the regular expression](https://regex101.com/r/kPpO2x/1)
197
+
176
198
## 2.4 Braces
177
199
178
200
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).
183
205
"[0-9]{2,3}" => The number was 9.<ahref="#learn-regex"><strong>999</strong></a>7 but we rounded it off to <ahref="#learn-regex"><strong>10</strong></a>.0.
184
206
</pre>
185
207
208
+
[Test the regular expression](https://regex101.com/r/juM86s/1)
209
+
186
210
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
187
211
the comma the regular expression `[0-9]{2}` means: Match exactly 2 digits.
188
212
189
213
<pre>
190
214
"[0-9]{2,}" => The number was 9.<ahref="#learn-regex"><strong>9997</strong></a> but we rounded it off to <ahref="#learn-regex"><strong>10</strong></a>.0.
191
215
</pre>
192
216
217
+
[Test the regular expression](https://regex101.com/r/Gdy4w5/1)
218
+
193
219
<pre>
194
220
"[0-9]{2}" => The number was 9.<ahref="#learn-regex"><strong>99</strong></a><ahref="#learn-regex"><strong>97</strong></a> but we rounded it off to <ahref="#learn-regex"><strong>10</strong></a>.0.
195
221
</pre>
196
222
223
+
[Test the regular expression](https://regex101.com/r/gqajq8/1)
224
+
197
225
## 2.5 Character Group
198
226
199
227
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
206
234
"(c|g|p)ar" => The <ahref="#learn-regex"><strong>car</strong></a> is <ahref="#learn-regex"><strong>par</strong></a>ked in the <ahref="#learn-regex"><strong>gar</strong></a>age.
207
235
</pre>
208
236
237
+
[Test the regular expression](https://regex101.com/r/tUxrBG/1)
238
+
209
239
## 2.6 Alternation
210
240
211
241
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
218
248
"(T|t)he|car" => <ahref="#learn-regex"><strong>The</strong></a> <ahref="#learn-regex"><strong>car</strong></a> is parked in <ahref="#learn-regex"><strong>the</strong></a> garage.
219
249
</pre>
220
250
251
+
[Test the regular expression](https://regex101.com/r/fBXyX0/1)
252
+
221
253
## 2.7 Escaping special character
222
254
223
255
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
230
262
"(f|c|m)at\.?" => The <ahref="#learn-regex"><strong>fat</strong></a> <ahref="#learn-regex"><strong>cat</strong></a> sat on the <ahref="#learn-regex"><strong>mat.</strong></a>
231
263
</pre>
232
264
265
+
[Test the regular expression](https://regex101.com/r/DOc5Nu/1)
266
+
233
267
## 2.8 Anchors
234
268
235
269
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`.
248
282
"(T|t)he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in <ahref="#learn-regex"><strong>the</strong></a> garage.
249
283
</pre>
250
284
285
+
[Test the regular expression](https://regex101.com/r/5ljjgB/1)
286
+
251
287
<pre>
252
288
"^(T|t)he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in the garage.
253
289
</pre>
254
290
291
+
[Test the regular expression](https://regex101.com/r/jXrKne/1)
292
+
255
293
### 2.8.2 Dollar
256
294
257
295
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.
262
300
"(at\.)" => The fat c<ahref="#learn-regex"><strong>at.</strong></a> s<ahref="#learn-regex"><strong>at.</strong></a> on the m<ahref="#learn-regex"><strong>at.</strong></a>
263
301
</pre>
264
302
303
+
[Test the regular expression](https://regex101.com/r/y4Au4D/1)
304
+
265
305
<pre>
266
-
"(at\.)$" => The fat cat sat on the m<ahref="#learn-regex"><strong>at.</strong></a>
306
+
"(at\.)$" => The fat cat. sat. on the m<ahref="#learn-regex"><strong>at.</strong></a>
267
307
</pre>
268
308
309
+
[Test the regular expression](https://regex101.com/r/t0AkOd/1)
310
+
269
311
## 3. Shorthand Character Sets
270
312
271
313
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
309
351
"(T|t)he(?=\sfat)" => <ahref="#learn-regex"><strong>The</strong></a> fat cat sat on the mat.
310
352
</pre>
311
353
354
+
[Test the regular expression](https://regex101.com/r/apqJZq/1)
355
+
312
356
### 4.2 Negative Lookahead
313
357
314
358
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
320
364
"(T|t)he(?!\sfat)" => The fat cat sat on <ahref="#learn-regex"><strong>the</strong></a> mat.
321
365
</pre>
322
366
367
+
[Test the regular expression](https://regex101.com/r/sswCvQ/1)
368
+
323
369
### 4.3 Positive Lookbehind
324
370
325
371
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`.
330
376
"(?<=(T|t)he\s)(fat|mat)" => The <ahref="#learn-regex"><strong>fat</strong></a> cat sat on the <ahref="#learn-regex"><strong>mat</strong></a>.
331
377
</pre>
332
378
379
+
[Test the regular expression](https://regex101.com/r/TZ8DOX/1/)
380
+
333
381
### 4.4 Negative Lookbehind
334
382
335
383
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`.
340
388
"(?<!(T|t)he\s)(cat)" => The cat sat on <ahref="#learn-regex"><strong>cat</strong></a>.
341
389
</pre>
342
390
391
+
[Test the regular expression](https://regex101.com/r/gleYg9/1)
392
+
343
393
## 5. Flags
344
394
345
395
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.
362
412
"The" => <ahref="#learn-regex"><strong>The</strong></a> fat cat sat on the mat.
363
413
</pre>
364
414
415
+
[Test the regular expression](https://regex101.com/r/dpQyf9/1)
416
+
365
417
<pre>
366
418
"/The/gi" => <ahref="#learn-regex"><strong>The</strong></a> fat cat sat on <ahref="#learn-regex"><strong>the</strong></a> mat.
367
419
</pre>
368
420
369
-
### 5.2 Global search
421
+
[Test the regular expression](https://regex101.com/r/ahfiuh/1)
370
422
423
+
### 5.2 Global search
371
424
372
425
The `g` modifier is used to perform a global match (find all matches rather than stopping after the first match). For example, the
373
426
regular expression`/.(at)/g` means: any character except new line, followed by lowercase character `a`, followed by lowercase
374
427
character `t`. Because we provided `g` flag at the end of the regular expression now it will find every matches from whole input
375
428
string.
376
429
377
-
378
-
379
430
<pre>
380
-
".(at)" => The <ahref="#learn-regex"><strong>fat</strong></a> cat sat on the mat.
431
+
"/.(at)/" => The <ahref="#learn-regex"><strong>fat</strong></a> cat sat on the mat.
381
432
</pre>
382
433
434
+
[Test the regular expression](https://regex101.com/r/jnk6gM/1)
435
+
383
436
<pre>
384
437
"/.(at)/g" => The <ahref="#learn-regex"><strong>fat</strong></a> <ahref="#learn-regex"><strong>cat</strong></a> <ahref="#learn-regex"><strong>sat</strong></a> on the <ahref="#learn-regex"><strong>mat</strong></a>.
385
438
</pre>
386
439
440
+
[Test the regular expression](https://regex101.com/r/dO1nef/1)
441
+
387
442
### 5.3 Multiline
388
443
389
444
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
397
452
on the <ahref="#learn-regex"><strong>mat.</strong></a>
398
453
</pre>
399
454
455
+
[Test the regular expression](https://regex101.com/r/hoGMkP/1)
456
+
400
457
<pre>
401
458
"/.at(.)?$/gm" => The <ahref="#learn-regex"><strong>fat</strong></a>
0 commit comments