I'm using Java Regex, and trying to use Regex to capture data that is not placed between parenthesis, but am having a bit of trouble with lookaheads and lookbehinds. Here's what I have so far: (?<!\()([A-Z][a-z]?[1-9]*)(?![1-9])(?!\))
But it doesn't work when the parenthesis is padded by other letters. For example, I'm searching in this string:
Zn(C2H3Na4O2)2Pb4K3
.
The regex should theoretically capture Zn, Pb4, and K3
but according to regex101 I'm getting Zn, He, Na4, Pb4, and K3.
Is there a way to make the regex look ahead to where there is a close parenthesis, as long as there isn't an open parenthesis before it?
Some other examples:
(NH4)PO3
should output P and O3
- for this example, the current regex works.
C3H6O2
should output C3, H6, and O2
K4(H3Mn4)3O5(NH4)2
should output K4 and O5
only. Right now, the regex outputs K4, M, and O5.