@@ -222,8 +222,8 @@ expression `[f|c|m]at\.?` means: lowercase letter `f`, `c` or `m`, followed by l
222
222
## 2.7 Anchors
223
223
224
224
In regular expression to check if the matching symbol is the starting symbol or endnig symbol of the input string for this purpose
225
- we use anchors. Anchors are of two types: First type is Caret ` ^ ` that check if the matching character is the character of the input and the
226
- second type is Dolar ` $ ` that checks if matching character is the last character of the input string.
225
+ we use anchors. Anchors are of two types: First type is Caret ` ^ ` that check if the matching character is the start character of the
226
+ input and the second type is Dolar ` $ ` that checks if matching character is the last character of the input string.
227
227
228
228
### 2.7.1 Caret
229
229
@@ -258,7 +258,7 @@ must be end of the string.
258
258
## 3. Shorthand Character Sets
259
259
260
260
Regular expression provides shorthands for the commonly used character sets, which offer convenient shorthands for commonly used
261
- regular expressions The shorthand character sets are as follows:
261
+ regular expressions. The shorthand character sets are as follows:
262
262
263
263
| Shorthand| Description|
264
264
| :----:| ----|
@@ -270,7 +270,64 @@ regular expressions The shorthand character sets are as follows:
270
270
| \s| Matches whitespace character: ` [\t\n\f\r\p{Z}] ` |
271
271
| \S| Matches non-whitespace character: ` [^\s] ` |
272
272
273
- ## 4. Lookaheads
273
+ ## 4. Lookaround
274
+
275
+ Lookbehind and lookahead sometimes known as lookaround are specific type of *** non-capturing group*** (Use to match the pattern but not
276
+ included in matching list). Lookaheads are used when we have the condition that this pattern is preceded or followed by another certain
277
+ pattern. For example we want to get all numbers that are preceded by ` $ ` character from the following input string ` $4.44 and $10.88 ` .
278
+ We will use following regular expression ` (?<=\$)[0-9\.]* ` which means: get all the numbers which contains ` . ` character and preceded
279
+ by ` $ ` character. Following are the lookarounds that are used in regular expressions:
280
+
281
+ | Symbol| Description|
282
+ | :----:| ----|
283
+ | ?=| Positive Lookahead|
284
+ | ?!| Negative Lookahead|
285
+ | ?<=| Positive Lookbehind|
286
+ | ?<!| Negative Lookbehind|
287
+
288
+ ### 4.1 Positive Lookahead
289
+
290
+ The positive lookahead asserts that the first part of the expression must be followed by the lookahead expression. The returned match
291
+ only contains the text that is matched by the first part of the expression. To define a positive lookahead braces are used and within
292
+ those braces question mark with equal sign is used like this ` (?=...) ` . Lookahead expression is written after the equal sign inside
293
+ braces. For example the regular expression ` [T|t]he(?=\sfat) ` means: optionally match lowercase letter ` t ` or uppercase letter ` T ` ,
294
+ followed by letter ` h ` , followed by letter ` e ` . In braces we define positive lookahead which tells regular expression engine to match
295
+ ` The ` or ` the ` which are followed by the word ` fat ` .
296
+
297
+ <pre >
298
+ "[T|t]he(?=\sfat)" => <a href =" #learn-regex " ><strong >The</strong ></a > fat cat sat on the mat.
299
+ </pre >
300
+
301
+ ### 4.2 Negative Lookahead
302
+
303
+ Negative lookahead is used when we need to get all matches from input string that are not followed by a pattern. Negative lookahead
304
+ defined same as we define positive lookahead but the only difference is instead of equal ` = ` character we use negation ` ! ` character
305
+ i.e. ` (?!...) ` . Lets take a look at the following regular expression ` [T|t]he(?!\sfat) ` which means: get all ` The ` or ` the ` words from
306
+ input string that are not followed by the word ` fat ` precedes by a space character.
307
+
308
+ <pre >
309
+ "[T|t]he(?!\sfat)" => The fat cat sat on <a href =" #learn-regex " ><strong >the</strong ></a > mat.
310
+ </pre >
311
+
312
+ ### 4.3 Positive Lookbehind
313
+
314
+ Positive lookbehind is used to get all the matches that are preceded by a specific pattern. Positive lookbehind is denoted by
315
+ ` (?<=...) ` . For example the regular expression ` (?<=[T|t]he\s)(fat|mat) ` means: get all ` fat ` or ` mat ` words from input string that
316
+ are after the word ` The ` or ` the ` .
317
+
318
+ <pre >
319
+ "(?<=[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 >.
320
+ </pre >
321
+
322
+ ### 4.4 Negative Lookbehind
323
+
324
+ Negative lookbehind is used to get all the matches that are not preceded by a specific pattern. Negative lookbehind is denoted by
325
+ ` (?<!...) ` . For example the regular expression ` (?<![T|t]he\s)(cat) ` means: get all ` cat ` words from input string that
326
+ are after not after the word ` The ` or ` the ` .
327
+
328
+ <pre >
329
+ "(?< ; ![T|t]he\s)(cat)" => The cat sat on <a href =" #learn-regex " ><strong >cat</strong ></a >.
330
+ </pre >
274
331
275
332
## 5. Flags
276
333
@@ -334,3 +391,14 @@ line. And beacause of `m` flag now regular expression engine matches pattern at
334
391
cat <a href =" #learn-regex " ><strong >sat</strong ></a >
335
392
on the <a href =" #learn-regex " ><strong >mat.</strong ></a >
336
393
</pre >
394
+
395
+ ## Contribution
396
+
397
+ * Report issues
398
+ * Open pull request with improvements
399
+ * Spread the word
400
+ * Reach out to me directly at
[email protected] or
[ ![ Twitter URL
] ( https://img.shields.io/twitter/url/https/twitter.com/ziishaned.svg?style=social&label=Follow%20%40ziishaned )] ( https://twitter.com/ziishaned )
401
+
402
+ ## License
403
+
404
+ MIT ©
[ Zeeshan Ahmed
] ( mailto:[email protected] )
0 commit comments