• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

250
Vistas
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 3 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda