Regular expression for odd number a

I had a problem resolving the following exercise, and I would appreciate any help.

Let Σ = {a, b}. I need to give a regex for all lines containing an odd number.

thank you for your time

+4
source share
2 answers
b*(ab*ab*)*ab*

its main part (ab*ab*)*, which lists all the possibilities of an even number as. then finally an extra amust exist to make it odd.

note that this regular expression is equivalent to:

b*a(b*ab*a)*b*

these two constructions are in the form determined by the pump lemma:

http://en.wikipedia.org/wiki/Pumping_lemma


UPDATE:

@MahanteshMAmbi , aaabaaa. . grep, , .

$ echo aaabaaa | grep -P -o 'b*(ab*ab*)*ab*'
aaabaa
a

-o grep . , , . 5 a s, 1 a. , ​​ .

, , :

^b*(ab*ab*)*ab*$

:

$ echo aaabaaa | grep -P -q '^b*(ab*ab*)*ab*$'
$ echo $?
1
+5
^[^a]*a(?=[^a]*(?:a[^a]*a)*[^a]*$).*$

a's . . .

https://regex101.com/r/eS7gD7/22

0

All Articles