我正在尝试解析输入字符串,看起来像
array[digit or expression or array, digit or expression or array]
所以我需要在[ , ]
中获取值。我试图让他们使用这个正则表达式:
(array1)\[(.*)\,(.*)\]
获取(.*)
捕获组的值,但它不起作用,因为它是贪婪的量词,所以在以下情况下:
array1[ array2[4,3] , array2[1,6] ]
我将array2[4,3] , array2[1,
作为第一个捕获组,6
作为第二个不正确。
如何将array2[4,3]
作为第一个和array2[1,6]
作为第二个捕获组?如果输入字符串为array2[array3[1,1],3]
,则为5+3
和array1[ array2[array3[1,1],3] , 5+3 ]
答案 0 :(得分:3)
您可以使用平衡组:
array\d*\[\s*((?:[^\[\]]|(?<o>\[)|(?<-o>\]))+(?(o)(?!))),\s*((?:[^\[\]]|(?<o>\[)|(?<-o>\]))+(?(o)(?!)))\]
ideone demo在你的最后一个字符串上。
细分:
array\d*\[\s* # Match array with its number (if any), first '[' and any spaces
(
(?:
[^\[\]] # Match all non-brackets
|
(?<o>\[) # Match '[', and capture into 'o' (stands for open)
|
(?<-o>\]) # Match ']', and delete the 'o' capture
)+
(?(o)(?!)) # Fails if 'o' doesn't exist
)
,\s* # Match comma and any spaces
( # Repeat what was above...
(?:
[^\[\]] # Match all non-brackets
|
(?<o>\[) # Match '[', and capture into 'o' (stands for open)
|
(?<-o>\]) # Match ']', and delete the 'o' capture
)+
(?(o)(?!)) # Fails if 'o' doesn't exist
)
\] # Last closing brace