ferics
ferics

Reputation: 87

Regex that matches even amount of character

Disclamer (after solved): this is my uni assignment thus I the answer could be simple. Hints are shown but my answer is hidden from here. Alternative answers could be found here but I take no responsibility with any plagiarism with direct answers posted here.


Hi I'm having troubles with the following exercise

Find regex that strictly represents the language:

b^(m+1), such that m>=0, m mod 2 = 1

The language breaks down to words:

{bb,bbbb,bbbbbb,bbbbbbbb,...}

I have tried the following:

b(bbb)?(bb)*

But this also accepts

{bb,bbb,bbbb,bbbbb,...}

Is there a way to write it such one bit of expression is depended on the other? ie: (bb)* cannot be chosen if (bbb)? is chosen at once, then repeat the decision but allow the vice versa.

Any help would be appreciated. Thanks

Upvotes: 3

Views: 105

Answers (2)

Code Maniac
Code Maniac

Reputation: 37755

Update:- You can use

^(?:bb)+$

Regex Demo

Initial heading of question was --> Regex that matches odd amount of character

You can try this

^b(?:(?:b{2})+)?$

enter image description here

Regex Demo

Upvotes: 1

Emma
Emma

Reputation: 27723

My guess is that, this might be closer,

^(?:bb){1,}$

and your set might look like,

bb
bbbb
bbbbbb

not sure though. If your set was correct, expression can likely be modified.


also, b would not probably be in the set, since m=0 does not pass the second requirement.


If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


Upvotes: 0

Related Questions