I'm looking for a regex to extract the content between [
and ]
that may include escaped characters. Some examples are given below.
+------------------+----------------+
| Input | Output |
+------------------+----------------+
| [B] | B |
| [B][C][D] | B |
| [hello\t\tworld] | hello\t\tworld |
| [hello\n\nworld] | hello\n\nworld |
| [\\]] | \\] |
| [\\\\] | \\\\ |
| [x[y\\]z][foo] | x[y\\]z |
+------------------+----------------+
For strings like [B][C][D]
, returning the smallest match is fine, since the desired pattern p
will be matched iteratively as p+
. It seems like a negated positive lookbehind, but I don't know if such a thing exists (i.e., consume until you see a ]
not preceded by one or more \\
).