How to define a regular expression to feature the following language?
L = {w ∈ {a, b}* | w has an even number of b's}
I tried to create the related automaton:
and from that i tried to apply the algorithm to obtain regular espression from DFA and i get this formula: a*ba*b
.
Could this be the right answer?
You are close but you need a
a*
in the end of your pattern.You also need the anchors^
and$
for specifying the start and end of your string.Then you can put all of your regex within a capture group and use*
to match any even number ifb
, and ana*
for zero number ofb
:Note:
|
is a logical OR and makes your regex engine match(a*ba*ba*)*
ora*
.Debuggex Demo
You can also make it more elegant but as you are not familiar very much with regex first i suggested the preceding pattern.
For example the following will works :