How can I combine regexes?
EDIT: This if for exam preparation. The question is write a regex to find all strings that have an odd number of a’s and an even number of b’s?
i.e. instead of | for OR, I need a mechanism to emulate AND
I have two regexes:
1) to find odd number of a's:
^[^a]*a([^a]*a[^a]*a)*[^a]*$
2) to find even number of b's:
^([^b]*b[^b]*b)*[^b]*$
You can do this using lookahead expressions (here shown as a verbose regex since it really is hard to read, much more on a single line):
The last two lines can be dropped if you’re just validating – they simply match the entire string.