Regular expression searches exclude nested results

My document contains several instances of code blocks similar to:

{% highlight %}
//some code
{% endhighlight %}

In Atom.io, I am trying to write a regular expression search to capture them.

My first attempt:
{% highlight .* %}([\S\s]+){% endhighlight %}

The problem is that in one document there are several code blocks, as well as the first block of code to the last, all in one match.

I would like to exclude the character {:
{% highlight .* %}([^\{]+){% endhighlight %}

But the problem is that some of the code blocks contain valid characters {(for example, function(){ ... }).

+4
source share
3 answers

Use unwanted match:

{% highlight .* %}([\S\s]+?){% endhighlight %}
                          ^
+1
source

Karthik , {% highlight %} {% end highlight %}, [\s\S]*? , .

unrolling-the-loop, :

{% highlight %}([^{]*(?:{(?!% endhighlight %})[^{]*)*){% endhighlight %}

regex

, , .

:

  • {% highlight %} - {% highlight %}
  • ([^{]*(?:{(?!% endhighlight %})[^{]*)*) - 1 , {% endhighlight %}:
    • [^{]* - 0 , {
    • (?:{(?!% endhighlight %})[^{]*)* - 0 ....
      • {(?!% endhighlight %}) - { % endhighlight %}
      • [^{]* - 0 , {
  • {% endhighlight %} - {% endhighlight %}

, {% highlight %}([\s\S]*?){% endhighlight %}, " ". .

+1

In this regex, you can only get the contents {% highlight %} ... {% endhighlight %}:

(?<={% highlight %}).*(?={% endhighlight %})

Test: https://regex101.com/r/nX6wV8/1


Sorry for the failure, hope this helps you

New expression: https://regex101.com/r/qX2cA1/1

-2
source

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


All Articles