2

I've been trying to extract any text between two curly braces, but the thing is that this text can possibly contains others curly braces.

The regex I've been trying (see below) does not work as it does not stop to the ending curly brace.

Actual regex

/class\s+([A-Z]{1}[a-zA-Z0-9]+)\s+(?:<\s+([a-zA-Z0-9]+))?\s*{(.|\n|\r)*}/gm

And according to the sample below, it matches the rest of the text for the third group

Sample

class Main < Console {
  + main:Int () {
    !String hello = "Hello, World!";
    this.out(hello);
    >> 0;
  }

}

class Foobar {
  + foobar:None () {
    #Some code...
  }
}

Matches

Group 1: Main
Group 2: Console
Group 3:

Does anyone has an idea of how to achieve this (if it's even possible with only Regex)?

Tripesdeporc
  • 94
  • 1
  • 1
  • 14

1 Answers1

2

Try this:

/class\s+([A-Z]\w+)\s+(?:<\s+(\w+))?\s*{[\s\S]*?\n}/gm

regex101 demo

This is all the same except for the last bit, where instead of doing {(.|\n|\r)*}, I put {[\s\S]*?\n} instead. Let's break this down a bit:

  • [\s\S]*? matches literally everything, but lazily, so it doesn't skip over brackets between matches.
  • \n} only matches closing brackets with no spaces before them, so it doesn't match the intermediate closing brackets.

Hope this helped :)

Ethan
  • 4,295
  • 4
  • 25
  • 44
  • Wonderful! Maybe `a-zA-Z0-9` can be replaced by `\w` which simplifies the expression and contemplates the case of an identifier including underscores. – coterobarros Oct 09 '21 at 21:12
  • @coterobarros Fair point, class names can indeed have underscores – Ethan Oct 10 '21 at 21:55