Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

271
Vistas
Extracting the next line data of a file if regex pattern matches in Java

I want to extract the next line data of a text file if regex pattern matches in Java. I am able to detect and match the pattern data in a text file, But unable to print the next line of pattern data.

Test data:

*** Explorer
GenV Deno Znet

Regular Expression for matching the Explorer

[*\\+]+[\\s+]+[Explorer]+[:]

Kindly help me on how to get the next line if *** Explorer pattern is found.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You can use this regex with a capturing group for the next line after your search pattern:

[*]+\s+Explorer\R(.*)

Next line is captured in group #1

Regex Breakup:

  • [*]+\s+Explorer - Match your search pattern
  • \R - Match any newline character
  • (.*) - Match and captured full line in group #1

In Java use:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "[*]+\\s+Explorer\\R(.*)";
final String input = "*** Explorer\nGenV Deno Znet";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
    System.out.println("match: " + matcher.group(1));
}

RegEx Demo

over 4 years ago · Santiago Trujillo Denunciar

0

You can Use:

[*\s]+Explorer\n(.*?)$

enter image description here

Regex Demo

over 4 years ago · Santiago Trujillo Denunciar

0

Well, for starters the regex is not going to match "*** Explorer"

It will match "*** Explorer:"

If this is java, can't you just read the next line?

        while ((lineText = lineReader.readLine()) != null) {
            hasMatch = lineText.matches(regex);
            if(hasMatch) {
                lineText = lineReader.readLine();
                System.out.println(lineText);
            }
        }

Works for me.

over 4 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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda