diff --git a/README.md b/README.md index a17ecf4..94a6081 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ A regular expression is just a pattern of letters and digits that we use to perf "cat" => The cat sat on the mat +[Test the regular expression](https://regex101.com/r/FOq5Nb/1) + The regular expression `123` matches the string "123". The regular expression is matched against an input string by comparing each character in the regular expression to each character in the input string, one after another. Regular expressions are normally 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". "Cat" => The cat sat on the Cat +[Test the regular expression](https://regex101.com/r/jw4Vi6/1) + ## 2. Meta Characters 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`. ".ar" => The car parked in the garage. +[Test the regular expression](https://regex101.com/r/xc9GkU/1) + ## 2.2 Character set 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 "[Tt]he" => The car parked in the garage. +[Test the regular expression](https://regex101.com/r/2ITLQ4/1) + 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.
 "ar[.]" => A garage is a good place to park a car.
 
+[Test the regular expression](https://regex101.com/r/wL3xtE/1) + ### 2.2.1 Negated character set 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`. "[^c]ar" => The car parked in the garage. +[Test the regular expression](https://regex101.com/r/nNNlq3/1) ## 2.3 Repetitions @@ -142,6 +153,8 @@ character set. For example, the regular expression `[a-z]*` means: any number of "[a-z]*" => The car parked in the garage #21. +[Test the regular expression](https://regex101.com/r/7m8me5/1) + The `*` symbol can be used with the meta character `.` to match any string of characters `.*`. The `*` symbol can be used with the whitespace character `\s` to match a string of whitespace characters. For example, the expression `\s*cat\s*` means: zero or more 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. "\s*cat\s*" => The fat cat sat on the concatenation. +[Test the regular expression](https://regex101.com/r/gGrwuz/1) + ### 2.3.2 The Plus 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 "c.+t" => The fat cat sat on the mat. +[Test the regular expression](https://regex101.com/r/Dzf9Aa/1) + ### 2.3.3 The Question Mark 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`.
 "[T]he" => The car is parked in the garage.
 
+ +[Test the regular expression](https://regex101.com/r/cIg9zm/1) +
 "[T]?he" => The car is parked in the garage.
 
+[Test the regular expression](https://regex101.com/r/kPpO2x/1) + ## 2.4 Braces In regular expression braces that are also called quantifiers are used to specify the number of times that a @@ -183,6 +205,8 @@ characters in the range of 0 to 9). "[0-9]{2,3}" => The number was 9.9997 but we rounded it off to 10.0. +[Test the regular expression](https://regex101.com/r/juM86s/1) + 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 the comma the regular expression `[0-9]{2}` means: Match exactly 2 digits. @@ -190,10 +214,14 @@ the comma the regular expression `[0-9]{2}` means: Match exactly 2 digits. "[0-9]{2,}" => The number was 9.9997 but we rounded it off to 10.0. +[Test the regular expression](https://regex101.com/r/Gdy4w5/1) +
 "[0-9]{2}" => The number was 9.9997 but we rounded it off to 10.0.
 
+[Test the regular expression](https://regex101.com/r/gqajq8/1) + ## 2.5 Character Group 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 "(c|g|p)ar" => The car is parked in the garage. +[Test the regular expression](https://regex101.com/r/tUxrBG/1) + ## 2.6 Alternation 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 "(T|t)he|car" => The car is parked in the garage. +[Test the regular expression](https://regex101.com/r/fBXyX0/1) + ## 2.7 Escaping special character 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 "(f|c|m)at\.?" => The fat cat sat on the mat. +[Test the regular expression](https://regex101.com/r/DOc5Nu/1) + ## 2.8 Anchors 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`. "(T|t)he" => The car is parked in the garage. +[Test the regular expression](https://regex101.com/r/5ljjgB/1) +
 "^(T|t)he" => The car is parked in the garage.
 
+[Test the regular expression](https://regex101.com/r/jXrKne/1) + ### 2.8.2 Dollar 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. "(at\.)" => The fat cat. sat. on the mat. +[Test the regular expression](https://regex101.com/r/y4Au4D/1) +
-"(at\.)$" => The fat cat sat on the mat.
+"(at\.)$" => The fat cat. sat. on the mat.
 
+[Test the regular expression](https://regex101.com/r/t0AkOd/1) + ## 3. Shorthand Character Sets 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 "(T|t)he(?=\sfat)" => The fat cat sat on the mat. +[Test the regular expression](https://regex101.com/r/apqJZq/1) + ### 4.2 Negative Lookahead 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 "(T|t)he(?!\sfat)" => The fat cat sat on the mat. +[Test the regular expression](https://regex101.com/r/sswCvQ/1) + ### 4.3 Positive Lookbehind 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`. "(?<=(T|t)he\s)(fat|mat)" => The fat cat sat on the mat. +[Test the regular expression](https://regex101.com/r/TZ8DOX/1/) + ### 4.4 Negative Lookbehind 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`. "(?<!(T|t)he\s)(cat)" => The cat sat on cat. +[Test the regular expression](https://regex101.com/r/gleYg9/1) + ## 5. Flags 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. "The" => The fat cat sat on the mat. +[Test the regular expression](https://regex101.com/r/dpQyf9/1) +
 "/The/gi" => The fat cat sat on the mat.
 
-### 5.2 Global search +[Test the regular expression](https://regex101.com/r/ahfiuh/1) +### 5.2 Global search The `g` modifier is used to perform a global match (find all matches rather than stopping after the first match). For example, the regular expression`/.(at)/g` means: any character except new line, followed by lowercase character `a`, followed by lowercase character `t`. Because we provided `g` flag at the end of the regular expression now it will find every matches from whole input string. - -
-".(at)" => The fat cat sat on the mat.
+"/.(at)/" => The fat cat sat on the mat.
 
+[Test the regular expression](https://regex101.com/r/jnk6gM/1) +
 "/.(at)/g" => The fat cat sat on the mat.
 
+[Test the regular expression](https://regex101.com/r/dO1nef/1) + ### 5.3 Multiline 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 on the mat. +[Test the regular expression](https://regex101.com/r/hoGMkP/1) +
 "/.at(.)?$/gm" => The fat
                   cat sat
                   on the mat.
 
+[Test the regular expression](https://regex101.com/r/E88WE2/1) + ## Bonus * *Positive Integers*: `^\d+$`