Gives regular expressions which defines regular language and what does {1,2} mean

93 Views Asked by At

The question is give a regular expression which defines a regular language.

Question: The language over {0,1} consisting of all strings which either have length less than 3 or have 0 as their third character.

Answer: null|(0|1){1,2}|(0|1){2}0(0|1)*

I understand how to do part of it but I don't understand what the {1,2} and {2} means in the answer.

2

There are 2 best solutions below

0
On BEST ANSWER

It's an amount. (0|1){1,2) means between (inclusive) 1 and 2 instances of a 0 or 1. This covers the length less than 3 scenario.

{2} means exactly two. So you want exactly 2 instances of either a 0 or 1, then you want a 0, then any number (*) of 0s or 1s afterwards.

0
On

It appears that $x\{a,b\}$ matches between $a$ and $b$ occurences of $x$; and $x\{a\}$ is short for $x\{a,a\}$, i.e. exactly $a$ occurences of $x$.