正则表达式(正常或嵌套括号)

时间:2020-12-13 21:44:46

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.

因此,我对全新的正则表达式世界完全陌生。基本上,我正在使用Gedit API来创建一个新的自定义语言规范(从C#派生)用于语法高亮(对于来自Byond的DM)。在DM中的转义字符中,您必须使用[variable]作为转义语法,这很简单。但是,它也可以嵌套,例如[array / list [index]]。 (它可以无限嵌套。)我已经查看了其他问题,当他们询问嵌套括号时,它们只表示嵌套,而在这种情况下它可能是/或。

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://*.com/a/9580978/2303154 but like mentioned above, that only matches if there are no brackets inside.
  • \ [(?:(?!\ [| \])。)* \]产生结果“测试[测试[测试]测试]测试[测试]测试”。这来自https://*.com/a/9580978/2303154,但如上所述,只有在内部没有括号时才匹配。

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.

显然我不知道我在这里做的更复杂的匹配,但至少我了解更多来自其他来源的基本操作。

1 个解决方案

#1


1  

From @Chaos7Theory:

来自@ Chaos7Theory:

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

在阅读GtkSourceView的规范参考时,我发现它特意使用了PCRE。然后我用它作为领导。

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

深入研究并通过反复试验,我得到了它:

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

I hope this helps someone else in the future.

我希望将来可以帮助别人。

#1


1  

From @Chaos7Theory:

来自@ Chaos7Theory:

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

在阅读GtkSourceView的规范参考时,我发现它特意使用了PCRE。然后我用它作为领导。

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

深入研究并通过反复试验,我得到了它:

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

I hope this helps someone else in the future.

我希望将来可以帮助别人。