Home > Software design >  Regex match two letter only with one evenly occuring
Regex match two letter only with one evenly occuring

Time:10-10

The following expression will match strings with even occurences of 'c':

^[^c]*(c[^c]*c[^c]*)*$

ccar(Yes), ccarc (No), cccarc (Yes)...

I would like to edit it to only match the inputs with letters 'c' and 'd' only:

ccddd(Yes), dddddccc (No), cccddcdddd (Yes)...

The number of 'd' can be either even or odd.

Thank you.

CodePudding user response:

If it should only allow d as the alternative to c, replace [^c] with d.

^d*(cd*cd*)*$
  • Related