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
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,10 +110,10 @@ expression `[Tt]he` means: an uppercase `T` or lowercase `t`, followed by the le
110
110
"[Tt]he" => <ahref="#learn-regex"><strong>The</strong></a> car parked in <ahref="#learn-regex"><strong>the</strong></a> garage.
111
111
</pre>
112
112
113
-
Just like above example the regular expression `ar[.]` means: an lowercase character `a`, followed by letter `r`, followed by any character.
113
+
Just like above example the regular expression `ge[.]` means: a lowercase character `g`, followed by letter `e`, followed by `.` character.
114
114
115
115
<pre>
116
-
"ar[.]" => The car p<ahref="#learn-regex"><strong>ark</strong></a>ed in the g<ahref="#learn-regex"><strong>ara</strong></a>ge.
116
+
"ge[.]" => The car parked in the gara<ahref="#learn-regex"><strong>ge.</strong></a>
117
117
</pre>
118
118
119
119
### 2.2.1 Negated character set
@@ -211,23 +211,23 @@ We can also use the alternation `|` meta character inside character group. For e
211
211
In regular expression Vertical bar `|` is used to define alternation. Alternation is like a condition between multiple expressions. Now,
212
212
you maybe thinking that character set and alternation works the same way. But the big difference between character set and alternation
213
213
is that character set works on character level but alternation works on expression level. For example the regular expression
214
-
`[T|t]he|car` means: uppercase character `T` or lowercase `t`, followed by lowercase character `h`, followed by lowercase character `e`
214
+
`(T|t)he|car` means: uppercase character `T` or lowercase `t`, followed by lowercase character `h`, followed by lowercase character `e`
215
215
or lowercase character `c`, followed by lowercase character `a`, followed by lowercase character `r`.
216
216
217
217
<pre>
218
-
"[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.
218
+
"(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
219
</pre>
220
220
221
221
## 2.7 Escaping special character
222
222
223
223
Backslash `\` is used in regular expression to escape the next character. This allows to to specify a symbol as a matching character
224
224
including reserved characters `{ } [ ] / \ + * . $ ^ | ?`. To use a special character as a matching character prepend `\` before it.
225
225
For example the regular expression `.` is used to match any character except new line. Now to match `.` in an input string the regular
226
-
expression `[f|c|m]at\.?` means: lowercase letter `f`, `c` or `m`, followed by lowercase character `a`, followed by lowercase letter
226
+
expression `(f|c|m)at\.?` means: lowercase letter `f`, `c` or `m`, followed by lowercase character `a`, followed by lowercase letter
227
227
`t`, followed by optional `.` character.
228
228
229
229
<pre>
230
-
"[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>
230
+
"(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
231
</pre>
232
232
233
233
## 2.8 Anchors
@@ -241,29 +241,29 @@ input and the second type is Dollar `$` that checks if matching character is the
241
241
Caret `^` symbol is used to check if matching character is the first character of the input string. If we apply the following regular
242
242
expression `^a` (if a is the starting symbol) to input string `abc` it matches `a`. But if we apply regular expression `^b` on above
243
243
input string it does not match anything. Because in input string `abc` "b" is not the starting symbol. Let's take a look on another
244
-
regular expression `^[T|t]he` which means: uppercase character `T` or lowercase character `t` is the start symbol of the input string,
244
+
regular expression `^(T|t)he` which means: uppercase character `T` or lowercase character `t` is the start symbol of the input string,
245
245
followed by lowercase character `h`, followed by lowercase character `e`.
246
246
247
247
<pre>
248
-
"[T|t]he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in <ahref="#learn-regex"><strong>the</strong></a> garage.
248
+
"(T|t)he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in <ahref="#learn-regex"><strong>the</strong></a> garage.
249
249
</pre>
250
250
251
251
<pre>
252
-
"^[T|t]he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in the garage.
252
+
"^(T|t)he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in the garage.
253
253
</pre>
254
254
255
255
### 2.8.2 Dollar
256
256
257
257
Dollar `$` symbol is used to check if matching character is the last character of the input string. For example regular expression
258
-
`(at.)$` means: lowercase character `a`, followed by lowercase character `t`, followed by anything except new line and the matcher
258
+
`(at\.)$` means: a lowercase character `a`, followed by lowercase character `t`, followed by a `.` character and the matcher
259
259
must be end of the string.
260
260
261
261
<pre>
262
-
"(at.)" => The f<ahref="#learn-regex"><strong>at </strong></a>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>
262
+
"(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
263
</pre>
264
264
265
265
<pre>
266
-
"(at.)$" => The fat cat sat on the m<ahref="#learn-regex"><strong>at.</strong></a>
266
+
"(at\.)$" => The fat cat sat on the m<ahref="#learn-regex"><strong>at.</strong></a>
267
267
</pre>
268
268
269
269
## 3. Shorthand Character Sets
@@ -301,43 +301,43 @@ by `$` character. Following are the lookarounds that are used in regular express
301
301
The positive lookahead asserts that the first part of the expression must be followed by the lookahead expression. The returned match
302
302
only contains the text that is matched by the first part of the expression. To define a positive lookahead braces are used and within
303
303
those braces question mark with equal sign is used like this `(?=...)`. Lookahead expression is written after the equal sign inside
304
-
braces. For example the regular expression `[T|t]he(?=\sfat)` means: optionally match lowercase letter `t` or uppercase letter `T`,
304
+
braces. For example the regular expression `(T|t)he(?=\sfat)` means: optionally match lowercase letter `t` or uppercase letter `T`,
305
305
followed by letter `h`, followed by letter `e`. In braces we define positive lookahead which tells regular expression engine to match
306
306
`The` or `the` which are followed by the word `fat`.
307
307
308
308
<pre>
309
-
"[T|t]he(?=\sfat)" => <ahref="#learn-regex"><strong>The</strong></a> fat cat sat on the mat.
309
+
"(T|t)he(?=\sfat)" => <ahref="#learn-regex"><strong>The</strong></a> fat cat sat on the mat.
310
310
</pre>
311
311
312
312
### 4.2 Negative Lookahead
313
313
314
314
Negative lookahead is used when we need to get all matches from input string that are not followed by a pattern. Negative lookahead
315
315
defined same as we define positive lookahead but the only difference is instead of equal `=` character we use negation `!` character
316
-
i.e. `(?!...)`. Let's take a look at the following regular expression `[T|t]he(?!\sfat)` which means: get all `The` or `the` words from
316
+
i.e. `(?!...)`. Let's take a look at the following regular expression `(T|t)he(?!\sfat)` which means: get all `The` or `the` words from
317
317
input string that are not followed by the word `fat` precedes by a space character.
318
318
319
319
<pre>
320
-
"[T|t]he(?!\sfat)" => The fat cat sat on <ahref="#learn-regex"><strong>the</strong></a> mat.
320
+
"(T|t)he(?!\sfat)" => The fat cat sat on <ahref="#learn-regex"><strong>the</strong></a> mat.
321
321
</pre>
322
322
323
323
### 4.3 Positive Lookbehind
324
324
325
325
Positive lookbehind is used to get all the matches that are preceded by a specific pattern. Positive lookbehind is denoted by
326
-
`(?<=...)`. For example the regular expression `(?<=[T|t]he\s)(fat|mat)` means: get all `fat` or `mat` words from input string that
326
+
`(?<=...)`. For example the regular expression `(?<=(T|t)he\s)(fat|mat)` means: get all `fat` or `mat` words from input string that
327
327
are after the word `The` or `the`.
328
328
329
329
<pre>
330
-
"(?<=[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>.
330
+
"(?<=(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
331
</pre>
332
332
333
333
### 4.4 Negative Lookbehind
334
334
335
335
Negative lookbehind is used to get all the matches that are not preceded by a specific pattern. Negative lookbehind is denoted by
336
-
`(?<!...)`. For example the regular expression `(?<![T|t]he\s)(cat)` means: get all `cat` words from input string that
336
+
`(?<!...)`. For example the regular expression `(?<!(T|t)he\s)(cat)` means: get all `cat` words from input string that
337
337
are after not after the word `The` or `the`.
338
338
339
339
<pre>
340
-
"(?<![T|t]he\s)(cat)" => The cat sat on <ahref="#learn-regex"><strong>cat</strong></a>.
340
+
"(?<!(T|t)he\s)(cat)" => The cat sat on <ahref="#learn-regex"><strong>cat</strong></a>.
0 commit comments