Ross Drew Ross Drew -4 years ago 42
Java Question

Can regex extract a substring, specifically after another without including the first?

(Edit, I said 'digit', I should have said 'alphanumeric char')

How do I extract a postfix from a string from a list of possibles (

). All need to be preceded by a alphanumeric character to be valid but the character is not to be extracted:-

What I am using is
but this includes the preceding character (
) in the extracted value.

(Unit tests pass but it's not sophisticated enough to test the returned match)

Answer Source

Converting my comment to an answer.

You can use a negative lookahead instead of matching digit in your regex. Here is working regex:


RegEx Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download