What I'm trying to do: remove innermost unescaped square brackets surrounding a specific, unescaped character (\
is escape)
input: [\[x\]]\]\[[\[y\]]
output when looking for brackets around y: [\[x\]]\]\[\[y\]
output when looking for brackets around x: \[x\]\]\[[\[y\]]
In short, remove only the unescaped set of brackets around the specific character.
I tried this (for y): Regex.Replace(input, @"(?<!\\)\[(.*?(?<!\\)y.*?)(?<!\\)\]",@"$1"
, but that seems to match the first unescaped [
(before the x) with the last ]
. I figured I could replace the .
wildcards with a negating character class to exclude [
and ]
, but what I really need to negate is unescaped versions of these, and when I try to incorporate a negative lookbehind like (?<!\\)
in the negating character class, I seem to match nothing at all.
Thanks in advance for your time and effort.
edit:
To clarify, the contents of the unescaped square brackets can be anything (except another unescaped square bracket), as long as they contain the unescaped character of interest (y
). All the content of the brackets should remain.