^(a(aa)*1|(aa)+0)$
or
^(?:a(?:aa)*1|(?:aa)+0)$ if you use captures.
The first part: a(aa)*1 will correspond to any odd number a followed by one, and the second part: (aa)+0 will correspond to any even number a followed by zero.
You cannot track the number of matches of a template component in regular expressions. They have no memory. Fortunately, you can get around this limitation in this case.
source share