Using bracket for automata

91 Views Asked by At

What is the difference between (ab)* and [ab]* when we write in Automata regular expression? In my opinion, they both mean ab zero or more times, but they don't.

1

There are 1 best solutions below

0
user21283023 On

According to the POSIX Basic Regular Expression syntax, they differ as follows:

  • For square brackets ([ ]) you pick only one of the characters:
Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c"...
  • For round brackets (( )) characters are grouped together to act as a single entity:

    (Quote slightly adapted)

Defines a subexpression. It is treated as a single element. For example, ab* matches "a", "ab", "abb" and so on, while (ab)* matches "", "ab", "abab", "ababab", and so on.

Therefore, (ab)* works as you said, producing "ab" zero or more times.

But [ab]* would likely mean "either "a" zero or more times, or "b" zero or more times". In other words, (a*)|(b*).