Let’s learn Regular Expression -2
We already covered “Backslash, Pipe &
Question Mark.
Lets continue with next list expression
Lets continue with next list expression
· Parentheses ()
Parentheses () in Regular Expressions work the same way that
they do in mathematics.
Let’s try
with example,
/test(one|two)/cab
Now we
consider “Order of Operations” we used in math.
Without Parentheses, 2 + 3 x 5 = 17
With Parentheses, (2 + 3) x 5 = 25
This is
how paranthese impact the order of operation in math. Now let’s go to Regular
expression /test(one|two)/cab
this regex
to match either the cab page in testone or the thanks page in testtwo – and
it is
the parentheses that allow us to group so that the pipe knows what to choose
between.
Let’s do
one example.
we’re
going to roll two URLs into one goal, but this time, we use the parentheses to
tell the question mark what is optional. This website has three thank-you
pages:
thanks
thankyou
thanksalot
If we
only want the thanks and the thanksalot pages
to be part of our goal,
we could do it like this: thanks(alot)? This
means, the target string must include thanks,
but alot is optional. So it matches both thanks and thanksalot.
And the thankyou page will never get
included, because there is no s in
its URL so it doesn’t match the beginning of this RegEx, i.e.thanks.
· Square
Brackets & Dashes
With use
of Square Brackets, we can create a simple list, like this: [aiu] .
This is a list of items and includes three vowels only.
So, p[aiu]n will
match pan, pin and pun. But it will not match pain, because that would require
us to use two items from the [aiu] list, and that is not
allowed in this simple example. You can also use a dash to create a list
of items, like this:
[a-z] – all lower-case letters in the English alphabet
[A-Z] – all upper-case letters in the English Alphabet
[a-zA-Z0-9] – all lower-case and upper-case letters, and
digits.
Dashes are one way of creating a list of items quickly, as you can
see above. (Note that they are not separated by commas.)
· Braces {}
Braces
repeat the last “piece” of information a specific number of times.These can be used
with two numbers, like this: {1,3}, or with one number, like this: {3}.
{x,y }, : When
there are two numbers in the braces, such as {1,3}, it means, repeat the last
“item” at least 1 time and no more than 3 times.
{z}: When there is only
one number in the braces, such as {3}, it means, repeat the last item exactly 3
times
Let’s see
it by one example,
we have
a scenario where, a page has 10-digit mobiles number are displayed and we
have to match all the mobile number using regEx. We could write a regEx as below:
\d{10}:\d is basically used to match a single digit.
This
regEx exactly matches 10 digits as defined in the braces.
I will
continue this topic in another post. Please click in “Regular Expression 3” for
further list of expression.
To start
with regular expression learning, please go through previous posts by clicking
on these links.
Regular Expression 3
Happy
Testing!!!
Comments
Post a Comment