Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Combining "not" clauses in regex

Tags:

java

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?

like image 789
user941401 Avatar asked Sep 08 '26 08:09

user941401


2 Answers

/(?<!\|)\]/ 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 |

like image 75
Niet the Dark Absol Avatar answered Sep 10 '26 20:09

Niet the Dark Absol


/(?<!\|)\]/

Use negative lookbehind.

For java :

Pattern regex = Pattern.compile("(?<!\\|)\\[");
like image 28
FailedDev Avatar answered Sep 10 '26 21:09

FailedDev