I'm trying to find a substring that contains a "]" without a "|" in front of it. How can I possibly do this with regex?
3 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 |
5 Comments
NullUserException
@user941401 Yes: download.oracle.com/javase/6/docs/api/java/util/regex/…
FailedDev
@NullUserExceptionఠ_ఠ don't you need to double escape | and ] ?
NullUserException
@FailedDev Yes, but this is not my answer. I just added an explanation on what
?<! is.Alan Moore
Just FYI, the leading and trailing slashes are not part of the regex. Many people include them for traditional reasons, but I think that causes unnecessary confusion. The regex is
(?<!\|)\]; if you were using it in Perl you would probably write it as /(?<!\|)\]/, but in Java source code it should look like this: "(?<!\\|)\\]".Niet the Dark Absol
Thank you NullUserException for the added clarification - I was going to clarify but couldn't do so in a way that satisfied me >_>. And thank you Alan Moore for the further clarification.
Very simply: [^|]\].
If you also want to match a [ at the beginning of a string, use (^|[^|])\]
1 Comment
Amber
Won't match the string
"]".