Regex that match if the match contains a special word

I came across a problem with regex that I cannot solve. I want to combine part of the configuration file, but only if it contains a special word:

{{START}} {{CONF1}}blah blah ..{{ENDCONF1}} {{CONF2}}blah blah ..{{ENDCONF2}} {{END}} {{START}} {{CONF1}}blah blah ..{{ENDCONF1}} {{CONF2}}blah specialword ..{{ENDCONF2}} {{END}} {{START}} {{CONF1}}blah blah ..{{ENDCONF1}} {{CONF2}}blah blah ..{{ENDCONF2}} {{END}} 

here I want to combine the entire block containing the "special word"

 {{START}} {{CONF1}}blah blah ..{{ENDCONF1}} {{CONF2}}blah specialword ..{{ENDCONF2}} {{END}} 

playing with some drawing, I reached the exact opposite with the search boxes "everything that does not contain a space-time word", but not the opposite that I want: /

 {{START}}((?!specialword)[\s\S])*?{{END}} 

to be clear i want

 {{START}}[\s\S]*?{{END}} 

where is the part that matches

 [\s\S]*? 

must contain a “special word” to match the entire expression

+4
source share
1 answer

You are on the right track with forecast statements :

 {{START}}(?:(?!{{END}})[\s\S])*specialword(?:(?!{{END}})[\s\S])*{{END}} 

Explanation:

 {{START}} # Match {{START}} (?: # Match... (?!{{END}}) # ...as long as we haven't reached {{END}} yet: [\s\S] # any character )* # any number of times. specialword # Match "specialword" (?: # Match (as before)... (?!{{END}}) # whatever follows, unless it {{END}} [\s\S] )* {{END}} # Then finally match {{END}} 
+4
source

Source: https://habr.com/ru/post/1488054/


All Articles