Given a string such as aaa|bbb
, is it possible to use RegExp (let's say PCRE) to check if the number of a
s before the pipe is the same as the number of b
s after it? So, in this string:
aaa|bbb
a|b
|
aaaaa|bbbbb
should all be matched, while
aaa|b
aaa|bb
aaa|bbbb
|b
a|
aa|b
should not be matched.
Is there any way to achieve this using only RegExp? I've tried using lookarounds to match the pipe character only if the lookaround conditions are met, by doing something like:
aaa|bbb
^ ^
test
aaa|bbb
^ ^
test
aaa|bbb
^ ^
but I couldn't figure any way of going over the same area twice, due to the linearity of RegExp. It would be possible to implement using nested lookarounds, but then the number of nests would have to be hard-coded, which would mean it wouldn't work with any number of elements, only up to the amount of lookarounds that one nests by hand.