Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expressions (Normal OR Nested Brackets)

So I'm completely new to the overwhelming world of Regex. Basically, I'm using the Gedit API to create a new custom language specification (derived from C#) for syntax-highlighting (for DM from Byond). In escaped characters in DM, you have to use [variable] as an escaping syntax, which is simple enough. However, it could also be nested, such as [array/list[index]] for instance. (It could be nested infinitely.) I've looked through the other questions, and when they ask about nested brackets they only mean exclusively nested, whereas in this case it could be either/or.

Several attempts I've tried:

  • \[.*\] produces the result "Test [Test[Test] Test]Test[Test] Test"
  • \[.*?\] produces the result "Test [Test[Test] Test]Test [Test] Test"
  • \[(?:.*)\] produces the result "Test [Test[Test] Test]Test[Test] Test"
  • \[(?:(?!\[|\]).)*\] produces the result "Test [Test[Test] Test]Test[Test] Test". This is derived from https://stackoverflow.com/a/9580978/2303154 but like mentioned above, that only matches if there are no brackets inside.

Obviously I've no real idea what I'm doing here in more complex matching, but at least I understand more of the basic operations from other sources.

like image 232
Chaos7Theory Avatar asked May 06 '13 20:05

Chaos7Theory


1 Answers

From @Chaos7Theory:

Upon reading GtkSourceView's Specification Reference, I've figured out that it uses PCRE specifically. I then used that as a lead.

Digging into it and through trial-and-error, I got it to work with:

\[(([^\[\]]*|(?R))*)\]

I hope this helps someone else in the future.

like image 112
Stephan Avatar answered Nov 13 '22 13:11

Stephan