Let’s learn Regular Expression:
This is
the second post for regular expression
Now let’s discuss it more in learning way.
Secret of learning regular expression
1.start
creating it
2. make mistakes.
3. go
back to reading basic or books
4. and
re-write it
Means more
you write regex and make mistake and rectify them by rewriting it ..would make Regular
expression expert.
There
are some important factors or expressions we need to use while writing regex. I
am going discuss those pointers:
· Backslash
(\)
A
backslash “escapes” a character. A backslash provides a bridge between Regular
Expressions and plain text.
e.g. I need
to write a regex to find /testing?id=123
now we
know that ? is regex expression and
in this string we need to find ? as plain text.
Here backslash
comes in picture.
Regex to
capture above string /testing\?id=123
Now notice, I use backslash in front of ? due to which regex now treat ? as plain text
instead of regex expression. Which means backslash escape a character
Backslash can be used
to turn special regex characters into plain characters.
· Pipe (|)
The pipe
is the simplest of Regular Expressions. It means ‘or’. Notice that
the pipe matches everything on either side of it.
e.g Lets say we have a page response and you want match all
the occurrences of words named test
and devops. Here we can use
pipe expression
test | devops
· Question
Mark (?)
A
question mark means, “The last item is optional.”
e. g. we
have a page with combination of words “colour” and “color” and we need to create
regex to match the occurrence of all such words in the page.
we could
create an expression like this:
colou?r: while matching for the words, the question mark before checks
for the at
least zero or one occurrence of letter 'u' in the word.
With the
above expression, we will be able to match both ‘color’ and ‘colour’ from the
page.
I would
write more post on this topic , because this has many expressions to use.
To read
further please click on this link “Regular Expression 2” & " Regular Expression 3"
Happy
Testing!!!
Comments
Post a Comment