How to match string in regular expression?


Regular Expression in theory of computation means by finite number of strings to be matched.

In this expression, we know the limit of expression. For example, if we will talk about 10^10 , (0 + 10*) , (11)* and so on.

Now I will tell you simple trick to match string in regular expression if you do not know anything about this topic.

Question:

Which of the following strings would match the regular expression:

n+[4-6]*[xyz]?

I. n444y
Il. n6y
III. 3xyz
IV. n35z
V. n353535x
Vl. nnn5

Now use the simple trick, firstly,divide the given equation n+[4-6]*[xyz] into three different parts:

Part 1 : n (it is compulsory before the string )

Part 2 : [4 – 6 ] * which means we can take values between 4 to 6 i.e 4, 44,45,56,666,444 etc.

Part 3 : [xyz] (it is also compulsory)



Now move to the options;

I. n444y – yes , it is correct because it lies on above three parts.

Il. n6y – yes , it is correct because it lies on above three parts.

III. 3xyz – No, it violates the rule of part 1 and 2 i.e n is not present and 3 is out of range.
IV. n35z – No, it violates the rule of part 2 i.e  3 is out of range.
V. n353535x –  No, it violates the rule of part 2 i.e  3 is out of range.
Vl. nnn5 –  No, it violates the rule of part 1  because n can occur at once.

so here is the simple trick when you do not know more about regular expression.

Leave a comment