Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Capture one or more groups if exists (Java)

I want to capture groups matching a pattern where the input can contain this group one or more times.

Example:

input = 12361 randomstuff371 12 Mar 16 138more random381 stuff73f

I want to capture the "12 Mar 16".

From this I have easily used the regex:

pattern = (".*(\\d{2}\\s\\w+\\s\\d{2}).*");

However my trouble is that when the input can contain more than one of these groups, I'm not able to capture the subsequent matches.

Example:

input = randomstuff371 12 Mar 16 14 Jan 15 13 Feb 16 138more random381 stuff73f

Such that:

group 1 = 12 Mar 16
group 2 = 14 Jan 15
group 3 = 13 Feb 16

The number of these groups to match will always vary, and so I'm wondering if there is a regex that will work over inputs that contain 1 or more of these groups. I have tried:

pattern = (".*(\\d{2}\\s\\w+\\s\\d{2}\\s)+.*"); \\ Not sure about whitespace at the end

However It doesn't work. Is this more to do with how I'm storing these captured groups? I cannot predetermine the number of groups that I will need, especially since the regex needs to work over many of these inputs.

I feel as though I'm just better off capturing the entire segment of dates and handling it later with matcher.find() to count the number of groups that I require.

Any help will be much appreciated.

like image 660
J Z Avatar asked Mar 27 '17 11:03

J Z


1 Answers

It will be easier to just match the specific pattern of yours and get the substrings as multiple matches obtained using Matcher#find():

String s = "randomstuff371 12 Mar 16 14 Jan 15 13 Feb 16 138more random381 stuff73f";
Pattern pattern = Pattern.compile("\\b\\d{2}\\s\\w+\\s\\d{2}\\b");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    System.out.println(matcher.group(0)); 
} 

See the online Java demo and the regex demo.

I added word boundaries to the pattern to make sure the pattern is matched as a whole word, but they may be omitted if your substrings are glued to another text.

like image 148
Wiktor Stribiżew Avatar answered Nov 18 '22 23:11

Wiktor Stribiżew