简体   繁体   English

棘手的正则表达式,带有匹配的括号

[英]Tricky Regex with matching brackets

I'm trying to extract variable names from a large block of text. 我正在尝试从一大段文本中提取变量名。 The variables are identified by 2 opening and closing brackets [[varname]]. 变量由2个方括号[[varname]]标识。 The problem is that some of the variables are references to an index in an array [[varname[0]]]. 问题在于某些变量是对数组[[varname [0]]]中索引的引用。

I've tried \\[{2}(.+?)\\]{2,} . 我已经尝试过\\[{2}(.+?)\\]{2,} The matched text for the array vars is correct, but the text in the captured group is missing the trailing bracket. 数组vars的匹配文本正确,但是捕获的组中的文本缺少尾括号。 For example, in the string: 例如,在字符串中:

"This is an array var [[arrayvar[0]]]"

I'm trying to match [[arrayvar[0]]] get out arrayvar[0] via the group. 我正在尝试匹配[[arrayvar[0]]] arrayvar[0]通过组退出arrayvar[0]

Here's an example of what I'm trying to do https://regex101.com/r/lH1zQ0/4 这是我正在尝试做的一个例子https://regex101.com/r/lH1zQ0/4

Any ideas? 有任何想法吗?

The following one should suit your needs: 以下内容将满足您的需求:

\[\[(.*?(?:\[\d+\])?)\]\]

正则表达式可视化

Debuggex Demo Debuggex演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM