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

270
Views
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 answers
Answer question

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 Report

0

You can Use:

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

enter image description here

Regex Demo

over 4 years ago · Santiago Trujillo Report

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 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!