user941401
user941401

Reputation: 323

Java: Combining "not" clauses in regex

I'm trying to find a substring that contains a "]" without a "|" in front of it. How can I possibly do this with regex?

Upvotes: 3

Views: 108

Answers (3)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324630

/(?<!\|)\]/ is the regex you need.

?<! is a zero-width assertion also known as "negative lookbehind." This essentially means match ], but "look behind" and assert that the previous character isn't a |

Upvotes: 4

SLaks
SLaks

Reputation: 887433

Very simply: [^|]\].

If you also want to match a [ at the beginning of a string, use (^|[^|])\]

Upvotes: 1

FailedDev
FailedDev

Reputation: 26930

/(?<!\|)\]/

Use negative lookbehind.

For java :

Pattern regex = Pattern.compile("(?<!\\|)\\[");

Upvotes: 2

Related Questions