简体   繁体   中英

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]]. The problem is that some of the variables are references to an index in an array [[varname[0]]].

I've tried \\[{2}(.+?)\\]{2,} . The matched text for the array vars is correct, but the text in the captured group is missing the trailing bracket. 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.

Here's an example of what I'm trying to do https://regex101.com/r/lH1zQ0/4

Any ideas?

The following one should suit your needs:

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

正则表达式可视化

Debuggex Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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