简体   繁体   English

Java正则表达式最长匹配

[英]Java regular expression longest match

I am having a problem with a generic regex that matches (sort of) a typical string of the form 我遇到了与以下形式的典型字符串匹配(类似)的通用正则表达式问题

... "field1" "field2" "field3" "field4" ...

What I want to do is, of course, get each of these fields separately. 我要做的当然是分别获取每个字段。 Because the fields can contain any character, I am using a "catch-all" regex of the form 因为字段可以包含任何字符,所以我使用的是“ catch-all”正则表达式

... \"(.*?)\" +\"(.*?)\" +\"(.*?)\" +\"(.*?)\" + ...

The problem is, instead of producing 4 different groups, Java gives me just one, which is merges those 4 above, ie I get a single field: 问题是,Java没有给我提供四个不同的组,而是给了我一个,它合并了上面的四个,即我得到了一个字段:

field1" "field2" "field3" "field4

instead of 代替

field1
field2
field3
field4

I have even tried doing things like \\"([^\\"]*)\\" for each of the fields, but the result is the same. 我甚至尝试为每个字段执行\\“([^ \\”] *)\\“之类的操作,但结果是相同的。

How could I get these 4 fields separately? 如何分别获得这4个字段?

You may try String.split method for such inputs. 您可以尝试将String.split方法用于此类输入。

    String input = "... \"field1\" \"field2\" \"field3\" \"field4\" ...";
    String[] split = input.split("\"\\s*\"?");
    String field1 = split[1];  // field1
    String field2 = split[2];  // field2
    String field3 = split[3];  // field3
    String field4 = split[4];  // field4

Are you calling matcher.group(1), matcher.group(2), etc to get the individual matches? 您要调用matcher.group(1),matcher.group(2)等来获取单个匹配项吗? The default method returns the whole match which is all the fields. 默认方法返回整个匹配项,即所有字段。

Each call to matcher.find() will move to the next match: 每次对matcher.find()调用将移至下一个匹配项:

String input = "... \"field1\" \"field2\" \"field3\" \"field4\" ...";
Matcher matcher = Pattern.compile("\"(.*?)\"").matcher(input);
while (matcher.find())
    System.out.println(matcher.group(1));

or, if you really want to capture all four in one match: 或者,如果您真的想在一场比赛中同时捕捉全部四场比赛:

Matcher matcher = Pattern.compile("\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\".*?\"(.*?)\".*?").matcher(input);
if (matcher.find()) {
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
    System.out.println(matcher.group(3));
    System.out.println(matcher.group(4));
}

Both produce the same output, which is: 两者产生相同的输出,即:

field1
field2
field3
field4

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

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