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
+32-12Lines changed: 32 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,15 +35,15 @@ A regular expression is just a pattern of letters and digits that we used to sea
35
35
`cat` means: the letter `c`, followed by the letter `a`, followed by the letter `t`.
36
36
37
37
<pre>
38
-
"cat" => The <strong><ahref="#learn-regex">cat</a></strong> sat on the mat
38
+
"cat" => The <ahref="#learn-regex"><strong>cat</strong></a> sat on the mat
39
39
</pre>
40
40
41
41
The regular expression `123` matches the string "123". The regular expression is matched against an input string by comparing each
42
42
character in the regular expression to each character in the input string, one after another. Regular expressions are normally
43
43
case-sensitive so the regular expression `Cat` would not match the string "cat".
44
44
45
45
<pre>
46
-
"Cat" => The cat sat on the <strong><ahref="#learn-regex">Cat</a></strong>
46
+
"Cat" => The cat sat on the <ahref="#learn-regex"><strong>Cat</strong></a>
47
47
</pre>
48
48
49
49
## 2. Meta Characters
@@ -60,7 +60,7 @@ The meta character are as follows:
60
60
|*|Matches 0 or more repetitions of the preceding symbol.|
61
61
|+|Matches 1 or more repetitions of the preceding symbol.
62
62
|?|Makes the preceding symbol optional.|
63
-
|{n}|Braces. Matches “n” repetitions of the preceding symbol.|
63
+
|{n,m}|Braces. Matches at least "n" but not more than "m" repetitions of the preceding symbol.|
64
64
|(xyz)|Character group. Matches the characters xyz in that exact order.|
65
65
|||Alternation. Matches either the characters before or the characters after the symbol.|
66
66
|\|Escapes the next character. This allows you to match reserved characters <code>[] ( ) { } . * + ? ^ $ \ |</code>|
@@ -74,7 +74,7 @@ or new line characters. For example the regular expression `.ar` means: any char
74
74
letter `r`.
75
75
76
76
<pre>
77
-
".ar" => The <strong><ahref="#learn-regex">car</a></strong> <strong><ahref="#learn-regex">par</a></strong>ked in the <strong><ahref="#learn-regex">gar</a></strong>age.
77
+
".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.
78
78
</pre>
79
79
80
80
## 2.2 Character set
@@ -84,13 +84,13 @@ specify the characters range. The order of the character range inside square bra
84
84
expression `[Tt]he` means: an uppercase `T` or lowercase `t`, followed by the letter `h`, followed by the letter `e`.
85
85
86
86
<pre>
87
-
"[Tt]he" => <strong><ahref="#learn-regex">The</a></strong> car parked in <strong><ahref="#learn-regex">the</a></strong> garage.
87
+
"[Tt]he" => <ahref="#learn-regex"><strong>The</strong></a> car parked in <ahref="#learn-regex"><strong>the</strong></a> garage.
88
88
</pre>
89
89
90
90
Just like above example the regular expression `ar[.]` means: an lowercase character `a`, followed by letter `r`, followed by any character.
91
91
92
92
<pre>
93
-
"ar[.]" => The car p<strong><ahref="#learn-regex">ark</a></strong>ed in the g<strong><ahref="#learn-regex">ara</a></strong>ge.
93
+
"ar[.]" => The car p<ahref="#learn-regex"><strong>ark</strong></a>ed in the g<ahref="#learn-regex"><strong>ara</strong></a>ge.
94
94
</pre>
95
95
96
96
### 2.2.1 Negated character set
@@ -100,7 +100,7 @@ character set. For example the regular expression `[^c]ar` means: any character
100
100
the letter `r`.
101
101
102
102
<pre>
103
-
"[^c]ar" => The car <strong><ahref="#learn-regex">par</a></strong>ked in the <strong><ahref="#learn-regex">gar</a></strong>age.
103
+
"[^c]ar" => The car <ahref="#learn-regex"><strong>par</strong></a>ked in the <ahref="#learn-regex"><strong>gar</strong></a>age.
104
104
</pre>
105
105
106
106
@@ -116,7 +116,7 @@ of preceding lowercase character `a`. But if it apperas after a character set or
116
116
character set. For example the regular expression `[a-z]*` means: any number of lowercase letters in a row.
The `*` symbol can be used with the meta character `.` to match any string of characters `.*`. The `*` symbol can be used with the
@@ -125,7 +125,7 @@ spaces, followed by lowercase character `c`, followed by lowercase character `a`
125
125
zero or more spaces.
126
126
127
127
<pre>
128
-
"\s*cat\s*" => The fat<strong><ahref="#learn-regex"> cat </a></strong>sat on the <strong><ahref="#learn-regex">cat</a></strong>.
128
+
"\s*cat\s*" => The fat<ahref="#learn-regex"><strong> cat </strong></a>sat on the <ahref="#learn-regex"><strong>cat</strong></a>.
129
129
</pre>
130
130
131
131
### 2.3.2 The Plus
@@ -134,7 +134,7 @@ The symbol `+` matches one or more repetitions of the preceding character. For e
134
134
letter `c`, followed by any number of character, followed by the lowercase character `t`.
135
135
136
136
<pre>
137
-
"c.+t" => The fat <strong><ahref="#learn-regex">cat sat on the mat</a></strong>.
137
+
"c.+t" => The fat <ahref="#learn-regex"><strong>cat sat on the mat</strong></a>.
138
138
</pre>
139
139
140
140
### 2.3.3 The Question Mark
@@ -144,9 +144,29 @@ the preceding character. For example the regular expression `[T]?he` means: Opti
144
144
character `h`, followed by the lowercase character `e`.
145
145
146
146
<pre>
147
-
"[T]he" => <strong><ahref="#learn-regex">The</a></strong> car is parked in the garage.
147
+
"[T]he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in the garage.
148
148
</pre>
149
149
<pre>
150
-
"[T]?he" => <strong><ahref="#learn-regex">The</a></strong> car is parked in t<strong><ahref="#learn-regex">he</a></strong> garage.
150
+
"[T]?he" => <ahref="#learn-regex"><strong>The</strong></a> car is parked in t<ahref="#learn-regex"><strong>he</strong></a> garage.
151
151
</pre>
152
152
153
+
## 2.4 Braces
154
+
155
+
In regular expression braces that are also called quantifiers used to specify the number of times that a group of character or a
156
+
character can be repeated. For example the regular expression `[0-9]{2,3}` means: Match at least 2 digits but not more than 3 (
157
+
characters in the range of 0 to 9).
158
+
159
+
<pre>
160
+
"[0-9]{2}" => 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.
161
+
</pre>
162
+
163
+
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
164
+
the comma the regular expression `[0-9]{2}` means: Match exactly 2 digits.
165
+
166
+
<pre>
167
+
"[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.
168
+
</pre>
169
+
170
+
<pre>
171
+
"[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.
0 commit comments