Let’s learn Regular
Expression -3
Regular expression
is an interesting topic but very vast one.
This is my
third post for regular expression.
· Dot
(.)
A dot
matches any one character.
Ultimately
represents numeric, alpha, special character. And a dot even matches a
whitespace.
Let’s see
an example
.ar
It would
match car, tar, bar. It would also
match %ar and #ar (because % and # are
characters, too.)
However,
it wouldn’t match ar. Why not? A dot
matches one character, and ar
includes zero characters for the dot to match (i.e., it didn’t match any).
· The Plus
Sign (+)
A plus
sign matches one or more of the former items.
Let see
an example
Aaargh! :
Aaaargh!
Aaaaaaaaargh
argh
Now we write regex aa+rgh
That
will match aargh and aaargh and Aaaaaaaaargh
however,
that it won’t match argh since +
says one or more of the former items.
· Star
(*)
*
function just like plus signs, except they allow us to match ZERO (or more) of
the previous items
Lets consider
same example of + plus
Aa*rgh
It will match all four strings from above example
· Dot
Star (. *)
There
are two Regular Expressions that, when put together, mean “get everything.”
They are a dot followed by a star, like this:
testing. *one\.php
In this
example, regex will match to everything that starts with testing and ends with one.php .
This means if you have pages in the testing
directory that end with .html, they won’t be a match to the above RegEx.
· Caret (^)
a caret
in Regular Expression, force the Expression to match only strings that start
exactly the same way RegEx does.
Let’s see one example if website page contains
strings that start with a number at the beginning such as:
123test
114test
143test
We need
to match only the digits at the staring of the string.
let’s
write a regex:
(^\d+)
Here ^ indicates beginning of the string.
\d denotes matching a single digit.
+
denotes
matching of one or more digits.
· Dollar Sign ($)
A dollar
sign means doesn’t match if the target string has any characters beyond where I
have placed the dollar sign in my Regular Expression.
· Whitespace
(\s)
Matches
a whitespace character, which in ASCII are tab, line feed, form feed, carriage
return, and space; in Unicode, also matches no-break spaces, next line, and the
variable-width spaces (amongst others).
· Digits(\d)
Matches
a single digit,same as [0-9] in ASCII; When used with
combination of regular expression such as \d+ will match one or more digits in
a line or string.
To start
with regular expression learning, please go through previous posts by clicking
on these links.
Happy
Testing!!!
Comments
Post a Comment