As the title said, I need to make a regex for a number with at least three even digits and odd length.
As an example, the number 248 should match.
Thank you!
Regex with at least three even digits and odd length
regular expressions
regular expressions
As the title said, I need to make a regex for a number with at least three even digits and odd length.
As an example, the number 248 should match.
Thank you!
Best Answer
Let $e$ denote any even digit and $o$ any odd digit. Let
$$E=\big((e+o)(e+o)\big)^*$$
and
$$O=(e+o)E\,.$$
Then the expression
$$\begin{align*} &E(eee+eeEe+eEee+eEeEe+eOeOe)E\\ &\qquad+O(eee+eeEe+eEee+eEeEe+eOeOe)O\\ &\qquad+E(eeOe+eOee+eOeEe+eEeOe)O\\ &\qquad+O(eeOe+eOee+eOeEe+eEeOe)E \end{align*}$$
should do the trick.