正则表达式解析复杂的阵列

编程入门 行业动态 更新时间:2024-10-23 03:20:27
本文介绍了正则表达式解析复杂的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图解析输入字符串,看起来像数组[位或前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] ?

解决方案

您可以均衡组的使用:

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

更多推荐

正则表达式解析复杂的阵列

本文发布于:2023-11-28 15:34:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642980.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阵列   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!