Search code examples
regexalternation

Example of (a|b)* about Alternation , I am confused , Why are ab, ba in the result set?


Alternation is about Union, then if We have R={"a"} , S={"b"} , then R|S will be {"a", "b"}.

Why ab, ba are included there in (a|b)*?

I don't understand why

enter image description here enter image description here

Regular Expresion wikipedia

I think the result set should be
(a|b)* = {Ɛ,"a", "b", "aa","bb", "aaa", "bbb", ...}


Solution

  • The expression a|b matches either a or b.

    The expression (a|b)* matches for example (a|b) (a|b) (a|b). In each of these alternatives, you can choose individually whether to match a or b. You don't have to choose the same for all of them.

    The variant "either a* or b*" is written exactly as pronounced: a*|b*.