diff --git a/README.md b/README.md index 9651e15..ec9cd78 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ * [Tiếng Việt](translations/README-vn.md) * [فارسی](translations/README-fa.md) * [עברית](translations/README-he.md) - +* [Bahasa Indonesia](translations/README-id.md) ## What is Regular Expression? diff --git a/translations/README-id.md b/translations/README-id.md new file mode 100644 index 0000000..7fd6340 --- /dev/null +++ b/translations/README-id.md @@ -0,0 +1,601 @@ +<p align="center"> + <br/> + <a href="https://github.com/ziishaned/learn-regex"> + <img src="https://i.imgur.com/bYwl7Vf.png" alt="Learn Regex"> + </a> +</p> + + +## Translations: + +* [English](README.md) +* [German](translations/README-de.md) +* [Español](translations/README-es.md) +* [Français](translations/README-fr.md) +* [Português do Brasil](translations/README-pt_BR.md) +* [中文版](translations/README-cn.md) +* [日本語](translations/README-ja.md) +* [한국어](translations/README-ko.md) +* [Turkish](translations/README-tr.md) +* [Greek](translations/README-gr.md) +* [Magyar](translations/README-hu.md) +* [Polish](translations/README-pl.md) +* [Русский](translations/README-ru.md) +* [Tiếng Việt](translations/README-vn.md) +* [فارسی](translations/README-fa.md) +* [עברית](translations/README-he.md) +* [Bahasa Indonesia](translations/README-id.md) + +## Apa itu Ekspresi Reguler? + +<p> + <a href="https://gum.co/learn-regex"> + <img src="https://img.shields.io/badge/-Download%20PDF%20-0a0a0a.svg?style=flat&colorA=0a0a0a" alt="Download PDF"> + </a> +</p> + +> Ekspresi reguler adalah sekelompok karakter atau simbol yang digunakan untuk menemukan pola tertentu dalam sebuah teks. + +Ekspresi reguler adalah pola yang dicocokkan dengan string subjek dari +kiri ke kanan. +Ekspresi reguler digunakan untuk mengganti teks dalam string,memvalidasi formulir, mengekstraksi substring dari string berdasarkan kecocokan pola, dan masih banyak lagi. Istilah "ekspresi reguler" adalah seteguk, jadi kamu biasanya akan temukan istilah yang disingkat menjadi "regex" atau "regexp". + +Bayangkan Anda sedang menulis aplikasi dan Anda ingin menetapkan aturan kapan a +pengguna memilih nama pengguna mereka. Kami ingin mengizinkan nama pengguna berisi huruf, +angka, garis bawah, dan tanda hubung. Kami juga ingin membatasi jumlah karakter +di username agar tidak terlihat jelek. Kita dapat menggunakan ekspresi reguler berikut untuk +memvalidasi nama pengguna: + +<br/><br/> +<p align="center"> + <img src="../img/regexp-en.png" alt="Regular expression"> +</p> + +Ekspresi reguler di atas dapat menerima string `john_doe`, `jo-hn_doe` dan +`john12_as`. Itu tidak cocok dengan `Jo` karena string itu berisi huruf besar +surat dan juga terlalu pendek. + +## Daftar isi + +- [Pencocokan Dasar](#1-basic-matchers) +- [Karakter Meta](#2-meta-characters) + - [Tanda Titik](#21-the-full-stop) + - [Set Karakter](#22-character-sets) + - [Set Karakter yang Dinegasikan](#221-negated-character-sets) + - [Repetisi](#23-repetitions) + - [Tanda Bintang](#231-the-star) + - [Tanda Tambah](#232-the-plus) + - [Tanda Tanya](#233-the-question-mark) + - [Tanda Kurung besar](#24-braces) + - [Penangkapan group](#25-capturing-groups) + - [Non-Capturing Groups](#251-non-capturing-groups) + - [Alternasi](#26-alternation) + - [Karakter spesial](#27-escaping-special-characters) + - [Jangkar](#28-anchors) + - [Tanda Karet](#281-the-caret) + - [Tanda Dollar](#282-the-dollar-sign) +- [Shorthand Set Karakter](#3-shorthand-character-sets) +- [Lookarounds](#4-lookarounds) + - [Positive Lookahead](#41-positive-lookahead) + - [Negative Lookahead](#42-negative-lookahead) + - [Positive Lookbehind](#43-positive-lookbehind) + - [Negative Lookbehind](#44-negative-lookbehind) +- [Bendera](#5-flags) + - [Case Insensitive](#51-case-insensitive) + - [Global Search](#52-global-search) + - [Multiline](#53-multiline) +- [Greedy vs Lazy Matching](#6-greedy-vs-lazy-matching) + +## 1. Pencocokan Dasar + +Ekspresi reguler hanyalah pola karakter yang kita gunakan untuk melakukan +pencarian dalam teks. Misalnya, ekspresi reguler `the` berarti: huruf +`t`, diikuti huruf `h`, diikuti huruf `e`. +<pre> +"the" => The fat cat sat on <a href="#learn-regex"><strong>the</strong></a> mat. +</pre> + +[Uji Ekspresi reguler](https://regex101.com/r/dmRygT/1) + +Ekspresi reguler `123` cocok dengan string `123`. Ekspresi reguler adalah +dicocokkan dengan string input dengan membandingkan setiap karakter di reguler +ekspresi ke setiap karakter dalam string input, satu demi satu. Reguler +ekspresi biasanya peka huruf besar/kecil sehingga ekspresi reguler `The` akan +tidak cocok dengan string `the`. + +<pre> +"The" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on the mat. +</pre> + +[Uji Ekspresi reguler](https://regex101.com/r/1paXsy/1) + +## 2. Karakter Meta + +Karakter meta adalah blok bangunan ekspresi reguler. Meta +karakter tidak berdiri sendiri tetapi sebaliknya ditafsirkan dalam beberapa +cara spesial. Beberapa karakter meta memiliki arti khusus dan tertulis di dalamnya +tanda kurung siku. Karakter metanya adalah sebagai berikut: + +|Karakter Meat|Deskripsi| +|:----:|----| +|.|Titik cocok dengan karakter tunggal apa pun kecuali jeda baris.| +|[ ]|Kelas karakter. Cocok dengan karakter apa pun yang ada di antara tanda kurung siku.| +|[^ ]|Kelas karakter yang dinegasikan. Cocok dengan karakter apa pun yang tidak ada di antara tanda kurung siku| +|*|Mencocokkan 0 atau lebih pengulangan dari simbol sebelumnya.| +|+|Mencocokkan 1 atau lebih pengulangan dari simbol sebelumnya.| +|?|Menjadikan simbol sebelumnya opsional.| +|{n,m}|Braces. Cocok setidaknya "n" tetapi tidak lebih dari "m" pengulangan simbol sebelumnya.| +|(xyz)|Kelompok karakter. Mencocokkan karakter xyz dalam urutan yang tepat.| +|||AAlternasi. Cocok dengan karakter sebelum atau karakter setelah simbol.| +|\|Escape karakter berikutnya. Ini memungkinkan Anda untuk mencocokkan karakter yang dipesan <code>[ ] ( ) { } . * + ? ^ $ \ |</code>| +|^|Cocok dengan awal input.| +|$|Cocok dengan akhir input.| + +## 2.1 Tanda Titik + +Tanda titik `.` adalah contoh paling sederhana dari karakter meta. Karakter meta `.` +cocok dengan karakter tunggal apa pun. Itu tidak akan cocok dengan karakter kembali atau baris baru. +Misalnya, ekspresi reguler `.ar` berarti: karakter apa pun, diikuti oleh +huruf `a`, diikuti dengan huruf `r`. + +<pre> +".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. +</pre> + +[Uji Ekspresi Reguler](https://regex101.com/r/xc9GkU/1) + +## 2.2 Character Sets + +Character sets are also called character classes. Square brackets are used to +specify character sets. Use a hyphen inside a character set to specify the +characters' range. The order of the character range inside the square brackets +doesn't matter. For example, the regular expression `[Tt]he` means: an uppercase +`T` or lowercase `t`, followed by the letter `h`, followed by the letter `e`. + +<pre> +"[Tt]he" => <a href="#learn-regex"><strong>The</strong></a> car parked in <a href="#learn-regex"><strong>the</strong></a> garage. +</pre> + +[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 the letter `r`, +followed by a period `.` character. + +<pre> +"ar[.]" => A garage is a good place to park a c<a href="#learn-regex"><strong>ar.</strong></a> +</pre> + +[Test the regular expression](https://regex101.com/r/wL3xtE/1) + +### 2.2.1 Negated Character Sets + +In general, the caret symbol represents the start of the string, but when it is +typed after the opening square bracket it negates the character set. For +example, the regular expression `[^c]ar` means: any character except `c`, +followed by the character `a`, followed by the letter `r`. + +<pre> +"[^c]ar" => The car <a href="#learn-regex"><strong>par</strong></a>ked in the <a href="#learn-regex"><strong>gar</strong></a>age. +</pre> + +[Test the regular expression](https://regex101.com/r/nNNlq3/1) + +## 2.3 Repetitions + +The meta characters `+`, `*` or `?` are used to specify how many times a +subpattern can occur. These meta characters act differently in different +situations. + +### 2.3.1 The Star + +The `*` symbol matches zero or more repetitions of the preceding matcher. The +regular expression `a*` means: zero or more repetitions of the preceding lowercase +character `a`. But if it appears after a character set or class then it finds +the repetitions of the whole character set. For example, the regular expression +`[a-z]*` means: any number of lowercase letters in a row. + +<pre> +"[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. +</pre> + +[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 a lowercase `c`, +followed by a lowercase `a`, followed by a lowercase `t`, +followed by zero or more spaces. + +<pre> +"\s*cat\s*" => The fat<a href="#learn-regex"><strong> cat </strong></a>sat on the con<a href="#learn-regex"><strong>cat</strong></a>enation. +</pre> + +[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: a lowercase `c`, followed by +at least one character, followed by a lowercase `t`. It needs to be +clarified that`t` is the last `t` in the sentence. + +<pre> +"c.+t" => The fat <a href="#learn-regex"><strong>cat sat on the mat</strong></a>. +</pre> + +[Test the regular expression](https://regex101.com/r/Dzf9Aa/1) + +### 2.3.3 The Question Mark + +In regular expressions, the meta character `?` makes the preceding character +optional. This symbol matches zero or one instance of the preceding character. +For example, the regular expression `[T]?he` means: Optional uppercase +`T`, followed by a lowercase `h`, followed by a lowercase `e`. + +<pre> +"[T]he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in the garage. +</pre> + +[Test the regular expression](https://regex101.com/r/cIg9zm/1) + +<pre> +"[T]?he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in t<a href="#learn-regex"><strong>he</strong></a> garage. +</pre> + +[Test the regular expression](https://regex101.com/r/kPpO2x/1) + +## 2.4 Braces + +In regular expressions, braces (also called quantifiers) are used to +specify the number of times that a character or a group of characters can be +repeated. For example, the regular expression `[0-9]{2,3}` means: Match at least +2 digits, but not more than 3, ranging from 0 to 9. + +<pre> +"[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. +</pre> + +[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]{3}` means: Match exactly 3 digits. + +<pre> +"[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. +</pre> + +[Test the regular expression](https://regex101.com/r/Gdy4w5/1) + +<pre> +"[0-9]{3}" => The number was 9.<a href="#learn-regex"><strong>999</strong></a>7 but we rounded it off to 10.0. +</pre> + +[Test the regular expression](https://regex101.com/r/Sivu30/1) + +## 2.5 Capturing Groups + +A capturing group is a group of subpatterns that is written inside parentheses +`(...)`. As discussed before, in regular expressions, if we put a quantifier +after a character then it will repeat the preceding character. But if we put a quantifier +after a capturing group then it repeats the whole capturing group. For example, +the regular expression `(ab)*` matches zero or more repetitions of the character +"ab". We can also use the alternation `|` meta character inside a capturing group. +For example, the regular expression `(c|g|p)ar` means: a lowercase `c`, +`g` or `p`, followed by `a`, followed by `r`. + +<pre> +"(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. +</pre> + +[Test the regular expression](https://regex101.com/r/tUxrBG/1) + +Note that capturing groups do not only match, but also capture, the characters for use in +the parent language. The parent language could be Python or JavaScript or virtually any +language that implements regular expressions in a function definition. + +### 2.5.1 Non-Capturing Groups + +A non-capturing group is a capturing group that matches the characters but +does not capture the group. A non-capturing group is denoted by a `?` followed by a `:` +within parentheses `(...)`. For example, the regular expression `(?:c|g|p)ar` is similar to +`(c|g|p)ar` in that it matches the same characters but will not create a capture group. + +<pre> +"(?: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. +</pre> + +[Test the regular expression](https://regex101.com/r/Rm7Me8/1) + +Non-capturing groups can come in handy when used in find-and-replace functionality or +when mixed with capturing groups to keep the overview when producing any other kind of output. +See also [4. Lookaround](#4-lookaround). + +## 2.6 Alternation + +In a regular expression, the vertical bar `|` is used to define alternation. +Alternation is like an OR statement between multiple expressions. Now, you may be +thinking that character sets and alternation work the same way. But the big +difference between character sets and alternation is that character sets work at the +character level but alternation works at the expression level. For example, the +regular expression `(T|t)he|car` means: either (an uppercase `T` or a lowercase +`t`, followed by a lowercase `h`, followed by a lowercase `e`) OR +(a lowercase `c`, followed by a lowercase `a`, followed by +a lowercase `r`). Note that I included the parentheses for clarity, to show that either expression +in parentheses can be met and it will match. + +<pre> +"(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. +</pre> + +[Test the regular expression](https://regex101.com/r/fBXyX0/1) + +## 2.7 Escaping Special Characters + +A backslash `\` is used in regular expressions to escape the next character. This +allows us to include reserved characters such as `{ } [ ] / \ + * . $ ^ | ?` as matching characters. To use one of these special character as a matching character, prepend it with `\`. + +For example, the regular expression `.` is used to match any character except a +newline. Now, to match `.` in an input string, the regular expression +`(f|c|m)at\.?` means: a lowercase `f`, `c` or `m`, followed by a lowercase +`a`, followed by a lowercase `t`, followed by an optional `.` +character. + +<pre> +"(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> +</pre> + +[Test the regular expression](https://regex101.com/r/DOc5Nu/1) + +## 2.8 Anchors + +In regular expressions, we use anchors to check if the matching symbol is the +starting symbol or ending symbol of the input string. Anchors are of two types: +The first type is the caret `^` that checks if the matching character is the first +character of the input and the second type is the dollar sign `$` which checks if a matching +character is the last character of the input string. + +### 2.8.1 The Caret + +The caret symbol `^` is used to check if a matching character is the first character +of the input string. If we apply the following regular expression `^a` (meaning 'a' must be +the starting character) to the string `abc`, it will match `a`. But if we apply +the regular expression `^b` to the above string, it will not match anything. +Because in the string `abc`, the "b" is not the starting character. Let's take a look +at another regular expression `^(T|t)he` which means: an uppercase `T` or +a lowercase `t` must be the first character in the string, followed by a +lowercase `h`, followed by a lowercase `e`. + +<pre> +"(T|t)he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in <a href="#learn-regex"><strong>the</strong></a> garage. +</pre> + +[Test the regular expression](https://regex101.com/r/5ljjgB/1) + +<pre> +"^(T|t)he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in the garage. +</pre> + +[Test the regular expression](https://regex101.com/r/jXrKne/1) + +### 2.8.2 The Dollar Sign + +The dollar sign `$` is used to check if a matching character is the last character +in the string. For example, the regular expression `(at\.)$` means: a +lowercase `a`, followed by a lowercase `t`, followed by a `.` +character and the matcher must be at the end of the string. + +<pre> +"(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> +</pre> + +[Test the regular expression](https://regex101.com/r/y4Au4D/1) + +<pre> +"(at\.)$" => The fat cat. sat. on the m<a href="#learn-regex"><strong>at.</strong></a> +</pre> + +[Test the regular expression](https://regex101.com/r/t0AkOd/1) + +## 3. Shorthand Character Sets + +There are a number of convenient shorthands for commonly used character sets/ +regular expressions: + +|Shorthand|Description| +|:----:|----| +|.|Any character except new line| +|\w|Matches alphanumeric characters: `[a-zA-Z0-9_]`| +|\W|Matches non-alphanumeric characters: `[^\w]`| +|\d|Matches digits: `[0-9]`| +|\D|Matches non-digits: `[^\d]`| +|\s|Matches whitespace characters: `[\t\n\f\r\p{Z}]`| +|\S|Matches non-whitespace characters: `[^\s]`| + +## 4. Lookarounds + +Lookbehinds and lookaheads (also called lookarounds) are specific types of +***non-capturing groups*** (used to match a pattern but without including it in the matching +list). Lookarounds are used when a pattern must be +preceded or followed by another pattern. For example, imagine we want to get all +numbers that are preceded by the `$` character from the string +`$4.44 and $10.88`. We will use the following regular expression `(?<=\$)[0-9\.]*` +which means: get all the numbers which contain the `.` character and are preceded +by the `$` character. These are the lookarounds that are used in regular +expressions: + +|Symbol|Description| +|:----:|----| +|?=|Positive Lookahead| +|?!|Negative Lookahead| +|?<=|Positive Lookbehind| +|?<!|Negative Lookbehind| + +### 4.1 Positive Lookahead + +The positive lookahead asserts that the first part of the expression must be +followed by the lookahead expression. The returned match only contains the text +that is matched by the first part of the expression. To define a positive +lookahead, parentheses are used. Within those parentheses, a question mark with +an equals sign is used like this: `(?=...)`. The lookahead expressions is written after +the equals sign inside parentheses. For example, the regular expression +`(T|t)he(?=\sfat)` means: match either a lowercase `t` or an uppercase + `T`, followed by the letter `h`, followed by the letter `e`. In parentheses we +define a positive lookahead which tells the regular expression engine to match `The` +or `the` only if it's followed by the word `fat`. + +<pre> +"(T|t)he(?=\sfat)" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on the mat. +</pre> + +[Test the regular expression](https://regex101.com/r/IDDARt/1) + +### 4.2 Negative Lookahead + +Negative lookaheads are used when we need to get all matches from an input string +that are not followed by a certain pattern. A negative lookahead is written the same way as a +positive lookahead. The only difference is, instead of an equals sign `=`, we +use an exclamation mark `!` to indicate negation 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 the input string that are not followed by a space character and the word `fat`. + +<pre> +"(T|t)he(?!\sfat)" => The fat cat sat on <a href="#learn-regex"><strong>the</strong></a> mat. +</pre> + +[Test the regular expression](https://regex101.com/r/V32Npg/1) + +### 4.3 Positive Lookbehind + +Positive lookbehinds are used to get all the matches that are preceded by a +specific pattern. Positive lookbehinds are written `(?<=...)`. For example, the +regular expression `(?<=(T|t)he\s)(fat|mat)` means: get all `fat` or `mat` words +from the input string that come after the word `The` or `the`. + +<pre> +"(?<=(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>. +</pre> + +[Test the regular expression](https://regex101.com/r/avH165/1) + +### 4.4 Negative Lookbehind + +Negative lookbehinds are used to get all the matches that are not preceded by a +specific pattern. Negative lookbehinds are written `(?<!...)`. For example, the +regular expression `(?<!(T|t)he\s)(cat)` means: get all `cat` words from the input +string that are not after the word `The` or `the`. + +<pre> +"(?<!(T|t)he\s)(cat)" => The cat sat on <a href="#learn-regex"><strong>cat</strong></a>. +</pre> + +[Test the regular expression](https://regex101.com/r/8Efx5G/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 combination, and are an +integral part of the RegExp. + +|Flag|Description| +|:----:|----| +|i|Case insensitive: Match will be case-insensitive.| +|g|Global Search: Match all instances, not just the first.| +|m|Multiline: Anchor meta characters work on each line.| + +### 5.1 Case Insensitive + +The `i` modifier is used to perform case-insensitive matching. For example, the +regular expression `/The/gi` means: an uppercase `T`, followed by a lowercase +`h`, followed by an `e`. And at the end of regular expression +the `i` flag tells the regular expression engine to ignore the case. As you can +see, we also provided `g` flag because we want to search for the pattern in the +whole input string. + +<pre> +"The" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on the mat. +</pre> + +[Test the regular expression](https://regex101.com/r/dpQyf9/1) + +<pre> +"/The/gi" => <a href="#learn-regex"><strong>The</strong></a> fat cat sat on <a href="#learn-regex"><strong>the</strong></a> mat. +</pre> + +[Test the regular expression](https://regex101.com/r/ahfiuh/1) + +### 5.2 Global Search + +The `g` modifier is used to perform a global match (finds all matches rather than +stopping after the first match). For example, the regular expression`/.(at)/g` +means: any character except a new line, followed by a lowercase `a`, +followed by a lowercase `t`. Because we provided the `g` flag at the end of +the regular expression, it will now find all matches in the input string, not just the first one (which is the default behavior). + +<pre> +"/.(at)/" => The <a href="#learn-regex"><strong>fat</strong></a> cat sat on the mat. +</pre> + +[Test the regular expression](https://regex101.com/r/jnk6gM/1) + +<pre> +"/.(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>. +</pre> + +[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 a pattern is at the beginning of the input or +the end. But if we want the anchors to work on each line, we use +the `m` flag. For example, the regular expression `/at(.)?$/gm` means: a lowercase +`a`, followed by a lowercase `t` and, optionally, anything except +a new line. And because of the `m` flag, the regular expression engine now matches patterns +at the end of each line in a string. + +<pre> +"/.at(.)?$/" => The fat + cat sat + on the <a href="#learn-regex"><strong>mat.</strong></a> +</pre> + +[Test the regular expression](https://regex101.com/r/hoGMkP/1) + +<pre> +"/.at(.)?$/gm" => The <a href="#learn-regex"><strong>fat</strong></a> + cat <a href="#learn-regex"><strong>sat</strong></a> + on the <a href="#learn-regex"><strong>mat.</strong></a> +</pre> + +[Test the regular expression](https://regex101.com/r/E88WE2/1) + +## 6. Greedy vs Lazy Matching +By default, a regex will perform a greedy match, which means the match will be as long as +possible. We can use `?` to match in a lazy way, which means the match should be as short as possible. + +<pre> +"/(.*at)/" => <a href="#learn-regex"><strong>The fat cat sat on the mat</strong></a>. </pre> + + +[Test the regular expression](https://regex101.com/r/AyAdgJ/1) + +<pre> +"/(.*?at)/" => <a href="#learn-regex"><strong>The fat</strong></a> cat sat on the mat. </pre> + + +[Test the regular expression](https://regex101.com/r/AyAdgJ/2) + + +## Contribution + +* Open a pull request with improvements +* Discuss ideas in issues +* Spread the word +* Reach out with any feedback [](https://twitter.com/ziishaned) + +## License + +MIT © [Zeeshan Ahmad](https://twitter.com/ziishaned)