用于解析复杂数组的正则表达式
本文关键字:正则表达式 数组 复杂 用于 | 更新日期: 2023-09-27 17:55:48
>我正在尝试解析输入字符串,看起来像 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 ]
?
您可以使用平衡组:
array'd*'['s*((?:[^'[']]|(?<o>'[)|(?<-o>']))+(?(o)(?!))),'s*((?:[^'[']]|(?<o>'[)|(?<-o>']))+(?(o)(?!)))']
在最后一个字符串上的 ideone 演示。
细分:
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