Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

416
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!