Skip to content

Commit 6b1ff1f

Browse files
committed
Add anchors
1 parent 63301d6 commit 6b1ff1f

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

README.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ contains uppercase letter and also it is too short.
3131
- [Character Group]()
3232
- [Alternation]()
3333
- [Escaping special character]()
34-
- [Quantifiers]()
35-
- [OR operator]()
36-
- [Character Sets]()
34+
- [Anchors]()
35+
- [Caret]()
36+
- [Dolar]()
3737
- [Shorthand Character Sets]()
38-
- [Grouping]()
3938
- [Lookaheads]()
4039
- [Flags]()
4140

@@ -215,4 +214,40 @@ expression `[f|c|m]at\.?` means: lowercase letter `f`, `c` or `m`, followed by l
215214

216215
<pre>
217216
"[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>
217+
</pre>
218+
219+
## 2.7 Anchors
220+
221+
In regular expression to check if the matching symbol is the starting symbol or endnig symbol of the input string for this purpose
222+
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
223+
second type is Dolar `$` that checks if matching character is the last character of the input string.
224+
225+
### 2.7.1 Caret
226+
227+
Caret `^` symbol is use to check if matching character is the first character of the input string. If we apply the following regular
228+
expression `^a` (if a is the starting symbol) to input string `abc` it matches `a`. But if we apply regular expression `^b` on above
229+
input string it does not match anything. Beacause in input string `abc` "b" is not the starting symbol. Lets take a look on another
230+
regular expression `^[T|t]he` which means: uppercase character `T` or lowercase character `t` is the start symbol of the input string,
231+
followed by lowercase character `h`, followed by lowercase character `e`.
232+
233+
<pre>
234+
"[T|t]he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in <a href="#learn-regex"><strong>the</strong></a> garage.
235+
</pre>
236+
237+
<pre>
238+
"^[T|t]he" => <a href="#learn-regex"><strong>The</strong></a> car is parked in the garage.
239+
</pre>
240+
241+
### 2.7.2 Dolar
242+
243+
Dolar `$` symbol is use to check if matching character is the last character of the input string. For example regular expression
244+
`(at.)$` means: lowercase character `a`, followed by lowercase character `t`, followed by anything except new line and the matcher
245+
must be end of the string.
246+
247+
<pre>
248+
"(at.)" => The f<a href="#learn-regex"><strong>at </strong></a>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>
249+
</pre>
250+
251+
<pre>
252+
"(at.)$" => The fat cat sat on the m<a href="#learn-regex"><strong>at.</strong></a>
218253
</pre>

0 commit comments

Comments
 (0)