Reputation: 323
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
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
Reputation: 887433
Very simply: [^|]\]
.
If you also want to match a [
at the beginning of a string, use (^|[^|])\]
Upvotes: 1
Reputation: 26930
/(?<!\|)\]/
Use negative lookbehind.
For java :
Pattern regex = Pattern.compile("(?<!\\|)\\[");
Upvotes: 2