Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

408
Visualizações
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.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda