From af92585e54d908cee11e1858007119bfc8f0c075 Mon Sep 17 00:00:00 2001 From: Aakash Panchal <51417248+Aakash-Panchal27@users.noreply.github.com> Date: Sun, 8 Mar 2020 12:58:14 +0530 Subject: [PATCH] Create boundary_matchers.html --- Akash Articles/RegEx/boundary_matchers.html | 186 ++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 Akash Articles/RegEx/boundary_matchers.html diff --git a/Akash Articles/RegEx/boundary_matchers.html b/Akash Articles/RegEx/boundary_matchers.html new file mode 100644 index 0000000..fa950d2 --- /dev/null +++ b/Akash Articles/RegEx/boundary_matchers.html @@ -0,0 +1,186 @@ + +
+ + + + + + + + + + + +Now, we will learn how to match patterns at specific positions, like before, after or between some characters. For this purpose we use special characters like ^
,$
,\b & \B
,\A
,\z & \Z
, which are known as anchors.
Notes:
+ +Line is a string which ends at a line-break or a new-line character \n
.
There is a slight change in javascript code, we were using up till now. Instead of /____/g
, we will now use /____/gm
. Modifier 'm' is used to perform multiline search. Notice it in next images!
Word character can be represented by, [A-Za-z0-9_]
.
Anchor ^
: It is used to match patterns at the very start of a line.
+ For example,
It will show a match, only if the pattern is occuring at the start of the line.
+ +Anchor $
: Similarly, $
is used to match patterns at the very end of a line.
It will show a match, only if the pattern is occuring at the end of a line.
+ +Example, both ^
and $
,
+
Anchors \b
& \B
: \b
is called word boundary character.
Below is a list of positions, which qualifies as a boundary for \b
:
+ If Regex-pattern is ending(or starting) with,
So, in short \b
is only looking for word-character at boundaries, so it is called word boundary character.
Let's first observe some examples to understand it's working:
What did you observe? Our regex-pattern is starting and ending with a word character. So, the match occurs only if there is a substring starting and ending at word characters, which are required in our regex [a-z]
and \d
respectively.
Now, let's look at one more example.
+ +<div class="container">
+
+
+
+
+
+
+ Here \+
will show a match for +
, check it out in appendix.
What did you observe? + First observation: Our pattern is starting with a non-word character and ending with a word character. So, the match occurs only if there is a substring having a non-word boundary at starting and word boundary at the ending.
+ +Second observation: Non-word character after a word-boundary does not affect the result.
+ +\b
need not be used in pair. You can use a single \b
.
\B
is just a complement of \b
. \B
matches at all the positions that is not a word boundary. Observe two examples below:
Note: \A
and \z & \Z
are another anchors, which are used to match at the very start of input text and at very end of input text respectively. But it is not supported in Javascript.
Predict the output of the following regex:
+ +^[\w$#%@!&^*]{6,18}$
This is matching passwords of length between 6 to 18:
+ Abfah45$
+ gadfaJ%33
+ Abjapda454&1 spc
+ bjaphgu12$
+
+ Answer: \b\w+:\B
1232: , +1232:, abc:, abc:a, abc89, (+abc::)
+ Answer: