正则表达式解析复杂的阵列 [英] Regex for parsing complicated array
问题描述
我试图解析输入字符串,看起来像数组[位或前pression或阵列,数字或前pression或阵列]
所以,我需要在来获取值[,]
。我试图让他们使用这个正则表达式:
(数组1)\\ [(。*)\\(。*)\\]
获得值
捕获组,但它doen't的工作,因为它是贪婪的量词,所以在的情况下,(*):
数组1 [数组2 [4,3]数组2 [1,6]
我将获得数组2 [4,3]数组2 [1,
作为第一个捕获组和 6
作为第二个这是不正确的。
我如何获得数组2 [4,3]
作为第一和数组2 [1,6]
作为第二捕获组?或数组2 [ARRAY3 [1,1],3]
和 5 + 3
如果输入字符串为数组1 [数组2 [ARRAY3 [1,1],3,5 + 3]
?
您可以均衡组的使用:
<$p$p><$c$c>array\\d*\\[\\s*((?:[^\\[\\]]|(?<o>\\[)|(?<-o>\\]))+(?(o)(?!))),\\s*((?:[^\\[\\]]|(?<o>\\[)|(?<-o>\\]))+(?(o)(?!)))\\]ideone演示你的最后一个字符串。
一个细分:
阵列\\ D * \\ [\\ S *#匹配阵列,它的编号(如果有的话),第一个'['和空格
(
(?:
[^ \\ [\\]#匹配所有非支架
|
(小于2 O&GT; \\ [)#匹配'[',并捕捉到O(代表开)
|
(?&LT; -O - GT; \\])#匹配']',并删除O捕获
)+
(?(O)(?!))#如果'O'不存在失败
)
,\\ S *#匹配逗号和空格
(#重复高于...
(?:
[^ \\ [\\]#匹配所有非支架
|
(小于2 O&GT; \\ [)#匹配'[',并捕捉到O(代表开)
|
(?&LT; -O - GT; \\])#匹配']',并删除O捕获
)+
(?(O)(?!))#如果'O'不存在失败
)
\\]#最后大括号
I'm trying parse input string which looks like
array[digit or expression or array, digit or expression or array]
So I need to get values in [ , ]
. I was trying to get them using this regex:
(array1)\[(.*)\,(.*)\]
to get values of (.*)
capturing groups, but it doen't work, because it's greedy quantifier, so in the case of:
array1[ array2[4,3] , array2[1,6] ]
I will get array2[4,3] , array2[1,
as first capturing group and 6
as a second which is not right.
How can I get array2[4,3]
as first and array2[1,6]
as second capturing group? Or array2[array3[1,1],3]
and 5+3
if the input string is array1[ array2[array3[1,1],3] , 5+3 ]
?
You can make use of balancing groups:
array\d*\[\s*((?:[^\[\]]|(?<o>\[)|(?<-o>\]))+(?(o)(?!))),\s*((?:[^\[\]]|(?<o>\[)|(?<-o>\]))+(?(o)(?!)))\]
ideone demo on your last string.
A breakdown:
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
这篇关于正则表达式解析复杂的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!