0

I am trying to match all tags except for tags inside textareas that have the "data-do-not-match-this='true'" attribute. Given I have this test string:

<textarea>{{one}}{{two}}</textarea> 

<textarea data-do-not-match-this="true">{{three}}{{four}}</textarea> 

<textarea>
{{five}}
{{six}}{{seven}}
</textarea> 

<textarea data-do-not-match-this="true">
{{eight}}
{{nine}}{{ten}}
</textarea>

{{eleven}}{{twelve}}

I have this regex so far:

(?<!data\-do\-not\-match\-this="true">)({{.*?}})

The regex incorrectly matches {{four}}, {{eight}}, {{nine}}, and {{ten}}. How can I fix the regex to exclude tags I do not want to match?

This is my Rubular:

https://rubular.com/r/TfjuwRd8dSjFJX

sjsc
  • 4,552
  • 10
  • 39
  • 55

2 Answers2

1

It was hard…

(?:<textarea data-do-not-match-this="true">.+?<\/textarea>.*?)?({{.*?}})

https://rubular.com/r/32MFMtZ2Ms3lOX

Inversion
  • 1,131
  • 13
  • 19
0

Here is one possibility:

<textarea[^>]+data-match-this="[^"]+"[^>]*>.*?<\/textarea>   

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Sorry Tim. I forgot to add tags outside of the textarea too. I modified the question above with the updated Rubular. – sjsc Jul 10 '19 at 14:39
  • How does your question change (I'm not seeing any change)? Can you edit your question and show us exactly what you are trying to match? – Tim Biegeleisen Jul 10 '19 at 14:40
  • Yes, does this Rubular make more sense: https://rubular.com/r/emay1eMo8FWlVQ? Basically, I'm trying to exclude all tags inside "textarea" blocks unless it has the data-match-this="true" attribute. If it's easier, we can modify the test string to use something like do-not-data-match-this="true" instead. – sjsc Jul 10 '19 at 14:46
  • My regex identifies all text which you want to keep, if I understand correctly. Then, why can't you just iterate your text file and retain only the portions which match? – Tim Biegeleisen Jul 10 '19 at 14:47