java regex - 捕获重复的组 [英] java regex - capture repeated groups

查看:144
本文介绍了java regex - 捕获重复的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java的正则表达式库。我想根据以下格式验证字符串:

  31,5,46,7,86(...)

数字量未知。我想确保该字符串中至少有一个数字,并且每两个数字用逗号分隔。我也想从字符串中获取数字。



注意:这只是一个简化的例子,string.split将无法解决我的问题实际问题)



我写了以下正则表达式:

 ({ [0-9] ++)((?:,[0-9] ++)* +)

验证部分有效。但是,当我尝试提取数字时,我得到2组:

  Group1:31 
Group2:,5 ,46,7,86

regex101版本: https://regex101.com/r/xJ5oQ6/3



有没有办法我可以分别得到每个号码吗?即结束收集:

  [31,5,46,7,86] 

提前致谢。

解决方案

Java不允许您访问重复捕获组的各个匹配项。有关更多信息,请查看以下问题:正则表达式 - 捕获所有重复组 / p>

Tim Pietzcker提供的代码也可以为您提供帮助。如果你稍微改写它并为第一个数字添加一个特殊情况你可以使用这样的东西:

  String target = 31,5,46,7,86\" ; 

模式compileFirst = Pattern.compile((?< number> [0-9] +)(,([0-9])+)*);
模式compileFollowing = Pattern.compile(,(?< number> [0-9] +));

Matcher matcherFirst = compileFirst.matcher(target);
Matcher matcherFollowing = compileFollowing.matcher(target);

System.out.println(matches:+ matcherFirst.matches());
System.out.println(first:+ matcherFirst.group(number));

int start = 0;
while(matcherFollowing.find(start)){
String group = matcherFollowing.group(number);

System.out.println(follow:+ start + - + group);
start = matcherFollowing.end();
}

此输出:

 匹配:true 
优先:31
以下:0 - 5
以下:4 - 46
以下:7 - 7
以下:9 - 86


I'm using Java's regex library. I want to validate a string against the following format:

31,5,46,7,86(...)

The amount of numbers is not known. I want to make sure there is at least one number in that string, and that every two numbers are separated by a comma. I also want to get the numbers from the string.

(Note: this is just a simplified example, string.split will not solve my actual problem)

I wrote the following regex:

({[0-9]++)((?:,[0-9]++)*+)

The validation part works. However, when I try to extract the numbers, I get 2 groups:

Group1: 31
Group2: ,5,46,7,86

regex101 version: https://regex101.com/r/xJ5oQ6/3

Is there a way I can get each number separately? i.e. to end up with the collection:

[31, 5, 46, 7, 86]

Thanks in advance.

解决方案

Java does not allow you to access the individual matches of a repeated capturing group. For more information look at this question: Regular Expression - Capturing all repeating groups

The code provided by Tim Pietzcker can help you as well. If you rework it a bit and add a special case for the first number you can use something like this:

String target = "31,5,46,7,86";

Pattern compileFirst = Pattern.compile("(?<number>[0-9]+)(,([0-9])+)*");
Pattern compileFollowing = Pattern.compile(",(?<number>[0-9]+)");

Matcher matcherFirst = compileFirst.matcher(target);
Matcher matcherFollowing = compileFollowing.matcher(target);

System.out.println("matches: " + matcherFirst.matches());
System.out.println("first: " + matcherFirst.group("number"));

int start = 0;
while (matcherFollowing.find(start)) {
    String group = matcherFollowing.group("number");

    System.out.println("following: " + start + " - " + group);
    start = matcherFollowing.end();
}

This outputs:

matches: true
first: 31
following: 0 - 5
following: 4 - 46
following: 7 - 7
following: 9 - 86

这篇关于java regex - 捕获重复的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆