无法缩小我的正则表达式来查找已定义的组

时间:2015-05-07 18:00:36

标签: java regex

我正在尝试制作一个解析以下结构的正则表达式,作为输入的示例:

  

插入{wine:Chateau Ste,年份:1997,原产地:法国,价值:500.00,qtt:3}

我能够通过以下代码实现这一目标:

String line = "insert {wine: Chateau Ste, year: 1997, origin: France, value: 500.00, qtd: 3}";
String pattern = "(^[a-zA-Z]+) \\{(\\w+): (.+), (\\w+): (.+), (\\w+): (.+), (\\w+): (.+), (\\w+): (.+)}";
Pattern r = Pattern.compile(pattern);

Matcher m = r.matcher(line);

if (m.find()) {
     System.out.println("Found value: " + m.group(0) );
     System.out.println("Found value: " + m.group(1) );
     System.out.println("Found value: " + m.group(2) );
     System.out.println("Found value: " + m.group(3) );
     System.out.println("Found value: " + m.group(4) );
     System.out.println("Found value: " + m.group(5) );
     System.out.println("Found value: " + m.group(6) );
     System.out.println("Found value: " + m.group(7) );
     System.out.println("Found value: " + m.group(8) );
     System.out.println("Found value: " + m.group(9) );
} else {
      System.out.println("NO MATCH");
}

作为输出,我得到:

Found value: insert {wine: Chateau Ste, year: 1997, origin: France, value: 500.00, qtd: 3}
Found value: insert
Found value: wine
Found value: Chateau Ste
Found value: year
Found value: 1997
Found value: origin
Found value: France
Found value: value
Found value: 500.00

然而,这种模式似乎过于重复和繁琐。我怎么能让它更紧凑?

我尝试了以下内容:

String pattern = "(^[a-zA-Z]+) \\{(([a-zA-Z]+): (.+), ){3}(\\w+): (.+)}";

但它给了我奇怪的结果和最后的例外:

Found value: insert {wine: Chateau Ste, year: 1997, origin: France, value: 500.00, qtd: 3}
Found value: insert
Found value: value: 500.00, 
Found value: value
Found value: 500.00
Found value: qtd
Found value: 3
Exception in thread "main" java.lang.IndexOutOfBoundsException: No group 7
    at java.util.regex.Matcher.group(Unknown Source)
    at testes.Teste.main(Teste.java:33)

1 个答案:

答案 0 :(得分:1)

你可以这样做。

String line = "insert {wine: Chateau Ste, year: 1997, origin: France, value: 500.00, qtd: 3}";
line = line.replaceAll("\\{"," ").replaceAll(":", " ").replaceAll(","," ").replaceAll("\\}"," ").trim();
for(String s:line.split("\\s+"))
     System.out.println(s);