Order of expressions in regular expressions?

1.5k Views Asked by At

Does the order of expressions in regular expressions matter?

For example:

Can I write for example $(a^*)b$ as:

$(a^*)b$ = $b(a^*)$ ?

What about the following?

  • $a^mb^n = b^na^m$
  • $a^nb^n=b^na^n$
  • $a^n b^m c^n = a^nc^nb^m$

If not then when does this order doesn't matter? E.g. in case of union and concatenation I learned it shouldn't matter i.e. $(a+b)=(b+a)$ and $ab=ba$?

1

There are 1 best solutions below

0
On BEST ANSWER

Concatenation is not commutative: if $\alpha$ and $\beta$ are finite strings of symbols, it is generally not true that $\alpha\beta=\beta\alpha$. For example, $a^*b$ represents the set of words $\{b,ab,aab,aaab,\ldots\}$, and $ba^*$ represents the set of words $\{b,ba,baa,baaa,\ldots\}$; clearly these are not the same set, so $a^*b\ne ba^*$. Your next three examples are similar: $a^mb^n$ is a string of $m$ $a$s followed by a string of $n$ $b$s, while $b^na^m$ is exactly the opposite, a string of $n$ $b$s followed by a string of $m$ $a$s. For instance, if $m=2$ and $n=3$ these are $aabbb$ and $bbbaa$, which are certainly not the same string. You should try to see why $a^mb^n=b^na^m$ if and only if at least one of $m$ and $n$ is $0$.

Union is commutative, so it’s quite true that the regular expressions $a+b$ and $b+a$ are equivalent; concatenation is not commutative, so the regular expressions $ab$ and $ba$ are not equivalent. If you’ve been that $ab=ba$, you’ve been taught incorrectly.