简体   繁体   English

使用Asterisk获得Group的匹配?

[英]Getting match of Group with Asterisk?

How can I get the content for a group with an asterisk? 如何获取带星号的组的内容?

For example I'd like to pare a comma separated list, eg 1,2,3,4,5 . 例如,我想削减以逗号分隔的列表,例如1,2,3,4,5

private static final String LIST_REGEX = "^(\\d+)(,\\d+)*$";
private static final Pattern LIST_PATTERN = Pattern.compile(LIST_REGEX);

public static void main(String[] args) {
    final String list = "1,2,3,4,5";
    final Matcher matcher = LIST_PATTERN.matcher(list);
    System.out.println(matcher.matches());
    for (int i = 0, n = matcher.groupCount(); i < n; i++) {
        System.out.println(i + "\t" + matcher.group(i));
    }
}

And the output is 输出是

true
0   1,2,3,4,5
1   1

How can I get every single entry, ie 1 , 2 , 3 , ...? 我怎样才能得到每一个条目,即123 ,...?

I am searching for a common solution. 我正在寻找一个共同的解决方案。 This is only a demonstrative example. 这只是一个示范性的例子。
Please imagine a more complicated regex like ^\\\\[(\\\\d+)(,\\\\d+)*\\\\]$ to match a list like [1,2,3,4,5] 请想象一个更复杂的正则表达式,如^\\\\[(\\\\d+)(,\\\\d+)*\\\\]$来匹配像[1,2,3,4,5]这样的列表

You can use String.split() . 您可以使用String.split()

for (String segment : "1,2,3,4,5".split(","))
    System.out.println(segment);

Or you can repeatedly capture with assertion: 或者您可以使用断言重复捕获:

Pattern pattern = Pattern.compile("(\\d),?");
for (Matcher m = pattern.matcher("1,2,3,4,5");; m.find())
     m.group(1);

For your second example you added you can do a similar match. 对于您添加的第二个示例,您可以执行类似的匹配。

for (String segment : "!!!!![1,2,3,4,5] //"
                          .replaceFirst("^\\D*(\\d(?:,\\d+)*)\\D*$", "$1")
                          .split(","))
    System.out.println(segment);

I made an online code demo . 我做了一个在线代码演示 I hope this is what you wanted. 我希望这是你想要的。


how can I get all the matches (zero, one or more) for a arbitary group with an asterisk (xyz)* ? 如何获得带有星号(xyz)*的任意组的所有匹配(零,一个或多个)? [The group is repeated and I would like to get every repeated capture.] [小组重复,我希望每次重复捕获。]

No, you cannot. 你不能。 Regex Capture Groups and Back-References tells why: 正则表达式捕获组和反向引用说明了原因:

The Returned Value for a Given Group is the Last One Captured 给定组的返回值是最后一个被捕获的

Since a capture group with a quantifier holds on to its number, what value does the engine return when you inspect the group? 由于具有量词的捕获组保持其数量,因此当您检查组时引擎返回什么值? All engines return the last value captured. 所有引擎都返回捕获的最后一个值。 For instance, if you match the string A_B_C_D_ with ([AZ]_)+ , when you inspect the match, Group 1 will be D_ . 例如,如果将字符串A_B_C_D_([AZ]_)+匹配,则在检查匹配时,组1将为D_ With the exception of the .NET engine, all intermediate values are lost. 除.NET引擎外,所有中间值都将丢失。 In essence, Group 1 gets overwritten each time its pattern is matched. 从本质上讲,每次匹配模式时,组1都会被覆盖。

I assume you may be looking for something like the following, this will handle both of your examples. 我假设您可能正在寻找以下内容,这将处理您的两个示例。

private static final String LIST_REGEX = "^\\[?(\\d+(?:,\\d+)*)\\]?$";
private static final Pattern LIST_PATTERN = Pattern.compile(LIST_REGEX);

public static void main(String[] args) {
    final String list = "[1,2,3,4,5]";
    final Matcher matcher = LIST_PATTERN.matcher(list);

    matcher.find(); 
    int i = 0;

    String[] vals = matcher.group(1).split(",");

    System.out.println(matcher.matches());
    System.out.println(i + "\t" + matcher.group(1));

    for (String x : vals) {
       i++;
       System.out.println(i + "\t" + x);
    }
}

Output 产量

true
0   1,2,3,4,5
1   1
2   2
3   3
4   4
5   5

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

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