Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to capture multiple number ranges (e.g. 1-6,25-41,55-70) with each range in its own group?

At the moment I have an Excel sheet with a column holding data in this form:

E 1-6,44-80
E 10-76
E 44-80,233-425
E 19-55,62-83,86-119,200-390
...

I need to be able to capture each range of numbers individually. For example, I would like the first line above to result in "1-6" and "44-80" being captured into their own groups.
So, essentially I need to capture a repeating group.

When trying to use this pattern, which uses the general form for capturing repeating groups given by @ssent1 on this question:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

E\s(([0-9]{1,4})-([0-9]{1,4}))((?:,([0-9]{1,4})-([0-9]{1,4}))*)

I end up only matching the first and last number ranges. I understand that this is because I’m repeating captured groups rather than capturing a repeating group, but I can’t figure out how to correct my pattern. Any help would be greatly appreciated.

>Solution :

In Java you can make use of a capture group and the \G anchor to get continuous matches:

(?:^E\h+|\G(?!^),?(\d{1,4}-\d{1,4}))

Regex demo | Java demo

Example

String regex = "(?:^E\\h+|\\G(?!^),?(\\d{1,4}-\\d{1,4}))";
String string = "E 1-6,44-80\n"
 + "E 10-76\n"
 + "E 44-80,233-425\n"
 + "E 19-55,62-83,86-119,200-390\n"
 + "200-390";

Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    if (matcher.group(1) != null) {
        System.out.println(matcher.group(1));
    }
}

Output

1-6
44-80
10-76
44-80
233-425
19-55
62-83
86-119
200-390
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading