简体   繁体   English

使用正则表达式解析数组语法

[英]Parsing array syntax using regex

I think what I am asking is either very trivial or already asked, but I have had a hard time finding answers. 我认为我要问的是非常琐碎的或已经问过的,但是我很难找到答案。

We need to capture the inner number characters between brackets within a given string. 我们需要捕获给定字符串中括号之间的内部数字字符。

so given the string 所以给定字符串

StringWithMultiArrayAccess[0][9][4][45][1]

and the regex 和正则表达式

^\w*?(\[(\d+)\])+?

I would expect 6 capture groups and access to the inner data. 我希望有6个捕获组并可以访问内部数据。 However, I end up only capturing the last "1" character in capture group 2. 但是,我最终只捕获捕获组2中的最后一个“ 1”字符。

If it is important heres my java junit test: 如果这很重要,这是我的java junit测试:

@Test
public void ensureThatJsonHandlerCanHandleNestedArrays(){
    String stringWithArr = "StringWithMultiArray[0][0][4][45][1]";
    Pattern pattern = Pattern.compile("^\\w*?(\\[(\\d+)\\])+?");


    Matcher matcher = pattern.matcher(stringWithArr);
    matcher.find();

    assertTrue(matcher.matches()); //passes

    System.out.println(matcher.group(2));  //prints 1 (matched from last array symbols)

    assertEquals("0", matcher.group(2)); //expected but its 1 not zero
    assertEquals("45", matcher.group(5));  //only 2 capture groups exist, the whole string and the 1 from the last array brackets

}

In order to capture each number, you need to change your regex so it (a) captures a single number and (b) is not anchored to--and therefore limited by--any other part of the string ("^\\w*?" anchors it to the start of the string). 为了捕获每个数字,您需要更改正则表达式,以便它(a)捕获单个数字,并且(b)不锚定于字符串的任何其他部分(因此不受其限制)(“ ^ \\ w * ?”将其锚定到字符串的开头)。 Then you can loop through them: 然后,您可以遍历它们:

Matcher mtchr = Pattern.compile("\\[(\\d+)\\]").matcher(arrayAsStr);
while(mtchr.find())  {
   System.out.print(mtchr.group(1) + " ");
}

Output: 输出:

0 9 4 45 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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