0

I have a negative lookbehind AND a negative lookahead and so far it's ALMOST working. I have googled, I have searched stackOverflow, but can not find my exact issue. What I am trying to achieve is to capture any text within brackets AS LONG AS they are not found between my <code></code> tags.

My regex attempt

(?<!<code>)\[(.*?)\](?!<\/code>)

Test Text

[bracket]                                           <-- currently matches (ok)
<code>[bracket]                                     <-- currently does NOT match (ok)
[bracket]</code>                                    <-- currently does NOT match (ok)
<code> some text [bracket] other text </code>       <-- currently matches (!!NOT OK!!)
<code>[bracket]</code>                              <-- currently does NOT match (ok)

I'm really having problems getting around the fact that you must have a fixed width in a negative lookbehind/negative lookahead. I've tried anything from trying to grab any character between the <code> and the bracket, but that just grabs the <code> tag and matches with almost all the examples above. I've tried the ^ character to try to exclude any lines with <code> but that ended up just excluding everything. I'm at my wits end. Any direction is truly appreciated!!

Anna
  • 135
  • 9
  • 1
    What about `foobar[bracket]`, with no ``? Or will that not occur? – CertainPerformance Sep 01 '20 at 04:21
  • 2
    This may be difficult to handle using regex, ideally you should use an XML/HTML parser. There are many edge cases here which would be hard to handle with regex alone. – Tim Biegeleisen Sep 01 '20 at 04:22
  • No, I only need to exclude the brackets that happen INSIDE `````` tags. It needs to have both the opening and closing tag. – Anna Sep 01 '20 at 04:23
  • In your examples `[bracket]` doesn't have closing tag and `[bracket]` doesn't have closing tag, so these `[brackets]` **are not** inside `...`. Why shouldn't they match? – Toto Sep 01 '20 at 09:29
  • It should extend multiple lines. – Anna Sep 01 '20 at 15:20

1 Answers1

1

You could use:

<code>[\s\S]*?</code>(*SKIP)(*FAIL)|\[.+?\]

Demo & explanation

Toto
  • 89,455
  • 62
  • 89
  • 125
  • I am not familiar with some of this syntax so I will do more research on these, but for the time being it looks like it works!!! Thank you so much! – Anna Sep 01 '20 at 15:24
  • @Anna: You're welcome, glad it helps. You'll find usefull info [here](https://stackoverflow.com/q/24534782/372239) – Toto Sep 01 '20 at 16:08