From 0048f664edb27959bc90af148e56ea7e1b40aca4 Mon Sep 17 00:00:00 2001 From: Aakash Panchal <51417248+Aakash-Panchal27@users.noreply.github.com> Date: Sun, 8 Mar 2020 12:55:17 +0530 Subject: [PATCH] Create Alternation.html --- Akash Articles/RegEx/Alternation.html | 114 ++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Akash Articles/RegEx/Alternation.html diff --git a/Akash Articles/RegEx/Alternation.html b/Akash Articles/RegEx/Alternation.html new file mode 100644 index 0000000..668819b --- /dev/null +++ b/Akash Articles/RegEx/Alternation.html @@ -0,0 +1,114 @@ + +
+ + + + + + + + +Character class can be used to match a single character out of several possible characters. Alternation is more generic than character class. It can also be used to match an expression out of several possible expressions.
+ +In the above example, cat|dog|lion
basically means 'either cat or dog or lion'. Here, we have used specific expression(cat, dog & lion), but we can use any regular expression. For example,
boot|bot
, b(o|oo)t
. Last expression is using a group.Suppose, you want to match two words Set and SetValue. What will be the regular expression?
+ +From whatever we have learned so far, you will say, Set|SetValue
will be the answer. But it is not correct.
If you try SetValue|Set
, then it is working.
Can you observe anything from it?
+ +OR operator tries to match a substring starting from the first word(or expression)-in the regex. If it is a match, then it will not try to match the next word(or expression) at the same place in text.
+ +Find out an regex which matches each and every word in the following set: {bat, cat, hat, mat, nat, oat, pat, Pat, ot}
. The regex should be as small as possible.
Hint: Use character-class, ranges and or-operator together.
+ +Answer: [b-chm-pP]at|ot