2

As far as this answer shows (Can regular expressions be used to match nested patterns?) we cannot create a regex that can be used to match a infinitely nested pattern. But can we create a regex pattern that matches up to N nested occurences of that pattern?

For example how could we create a regex that matches comments like this up to 3 layers deep?

   <*
      Comment of depth 1 containing another comment
      <* 
         That is a comment of depth 2 containing another comment
         <*
            Nanananananana BATMAN!!!
         *>
      *> 
    *>
Community
  • 1
  • 1
Fr0stBit
  • 1,455
  • 1
  • 13
  • 22

2 Answers2

0
<\*[\s\S]*?<\*[\s\S]*?<\*[\s\S]*?\*>[\s\S]*?\*>[\s\S]*?\*>

A simple way .See demo.

https://regex101.com/r/sJ9gM7/129

You can use recursive regex here.

<\*(?:(?R)|(?:(?!<\*|\*>)[\s\S])+)*\*>

See demo.

https://regex101.com/r/sJ9gM7/131

vks
  • 67,027
  • 10
  • 91
  • 124
0

You said in a comment that you will integrate the regex ain a BNF definition that will be fed up to the library "mpc".

Try to define the multiline comment like this:

mlcomment    : "<*" (<mlcomment> | /\*[^>]|[^*]/)* "*>" ;

I tried it with mpc and it works.

You can have comments not matter if they are nested. For example:

<* A multiline comment here
<* Another one here *> *>

The downside of this is that you cannot have multiple asterisks on the closing tag. This:

<* A multiline comment
second line **>

is not going to work. But I think it is as close as it gets with the limitations of mpc.

TheCrafter
  • 1,909
  • 2
  • 23
  • 44