Your answer is wrong because the parentheses do not belong to your set of symbols. The string (ab)aab
cannot be generated using only symbols present in the {a,b}
set.
Even more, you were asked to provide a string of 5 symbols but (ab)aab
has length 7
.
Parentheses have special meaning in regex
. They create sub-regexps and capturing groups. For example, (ab)*
means ab
can be matched any number of times, including zero. Without parentheses, ab*
means the regex
matches one a
followed by any number of b
s. That's a different expression.
For example:
- the regular expression
(ab)*
matches the empty string (ab
zero times), ab
, abab
, ababab
, abababab
and so on;
- the regular expression
ab*
matches a
(followed by zero b
s), ab
, abb
, abbb
, abbbb
and so on.
The first set of parentheses in your example is useless if you are looking only for sub-regexps. Both (ab)
and ab
expressions match only the ab
string. But they can be used to capture the matched part of the string and re-use it either with back references or for replacement.
When parentheses are used for sub-expressions in regular expressions, they are meta-characters, do not match anything in the string. In order to match an open parenthesis character (
(found in the string) you have to escape it in the regex
: \(
.
Several strings that match the regular expression (ab)(ab)*(aa|bb)*b
over Sigma = { 'a', 'b' }
: abb
, ababb
, abababababb
, ababababaabbaaaabbb
.
The last string (ababababaabbaaaabbb
) matches the regex
pieces as follows:
ab - (ab)
ababab - (ab)* - ('ab' 3 times)
aabbaaaabb - (aa|bb)* - ('aa' or 'bb', 5 times in total)
b - b
A regex
that matches the string (ab)aab
is \(ab\)(ab)*(aa|bb)*b
but in this case
Sigma = { 'a', 'b', '(', ')' }