Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find the regular expression?

I need a regular expression which will describe the strings of the alphabet {a, b, c} of the language {a^n b^m c^k, where n + m + k is even for variable integers n, m, k ≥ 0}. For example, the strings ε, aabc, abbccccc belong to the language, while the strings abccaa, a, aaabbcc
do not belong.

Should I build first a Finite Automaton or is it possible without it?

Any help will be appreciated!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Interpreting this as a programming question (which is what Stack Overflow is about), you could use this regular expression:

^(?=(..)*$)a*b*c*$

Explanation:

  • ^ and $ match the start/end of the input.
  • (..)* matches a series of pairs of any characters, so it always matches an even number of characters.
  • (?=(..)*$) is a look-ahead expression that verifies that the total length is even.
  • a*b*c* matches any number of a, followed by any number of b, followed by any number of c.

Without look-ahead, you’d need to foresee the cases where all groups are even, or just one group is even (and the two others odd):

^(aa)*((bb)*(bc)?(cc)*|a(bb)*[bc](cc)*)$

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading