1

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.

user3806019
  • 45
  • 2
  • 9
  • Please provide some good input and good output to further help. – SomeDude Mar 19 '17 at 21:11
  • I've added some more examples - is this what you're looking for? – user3806019 Mar 19 '17 at 21:14
  • 1
    Check out [this answer](http://stackoverflow.com/a/9030062/5527985). You can use a lookahead for checking if there's not a `)` ahead without any `(` in between: [`[A-Z][a-z]?\d*(?![^(]*\))`](https://regex101.com/r/Ggjjpw/1) for Java: `"[A-Z][a-z]?\\d*(?![^(]*\\))"` – bobble bubble Mar 19 '17 at 21:34

0 Answers0