Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why regular expression ((x,y)|(x,z)) is nondeterministic?

Tags:

java

regex

Why regular expression ((x,y)|(x,z)) is nondeterministic as the book "Core Java" said? The author gives his point:

When the parser sees x, it doesn’t know which of the two alternatives to take.This expression can be rewritten in a deterministic form as (x,(y|z))

Could anyone can give me an explanation?

like image 959
J.zhou Avatar asked Nov 24 '15 13:11

J.zhou


People also ask

Are regular expressions deterministic?

Intuitively, a regular expression is deterministic if, when matching a word from left to right with no lookahead, it is always clear where in the expression the next symbol must be matched.

What does () mean in regular expression?

The () will allow you to read exactly which characters were matched. Parenthesis are also useful for OR'ing two expressions with the bar | character. For example, (a-z|0-9) will match one character -- any of the lowercase alpha or digit.

What is regular expression explain with example?

A regular expression is a method used in programming for pattern matching. Regular expressions provide a flexible and concise means to match strings of text. For example, a regular expression could be used to search through large volumes of text and change all occurrences of "cat" to "dog".

What is the significance of operator in a regular expression?

The regular expression operators are used for matching patterns in searches and for replacements in substitution operations. The m operator is used for matching patterns, and the s operator is used when substituting one pattern for another.


1 Answers

To have a deterministic form, you are only allowed to have a maximum of one possible way at your current position. Let's say you have a string "x,y". Now the regex engine looks at the first character, the "x". In your expression you have 2 possibilities how your string can go on after an "x" at the first position to accept your input. Next there are 2 ways to check. Either if the string is followed by ",y" or by ",z".

   , ⇨ y
 ⬀
x
 ⬂
   , ⇨ z

For (x,(y|z)) you always have just one way. If an "x" is on position 1 you go to position 2. Same there, just with the ",". Finally he has to check if there is a "y" or a "z" on position 3 to accept the word. There were never 2 ways.

x ⇨ , ⇨ (y or z)
like image 82
Pinguin895 Avatar answered Oct 02 '22 07:10

Pinguin895