Nitesh Gupta
Nitesh Gupta

Reputation: 215

Search on a particular line using Regular Expression in Java

I am new with Regular Expression and might be my question is very basic one.

I want to create a regular expression that can search an expression on a particular line number.

eg. I have data

"\nerferf erferfre erferf 12545" + 
"\ndsf erf" + 
"\nsdsfd refrf refref" + 
"\nerferf erferfre erferf 12545" + 
"\ndsf erf" + 
"\nsdsfd refrf refref" + 
"\nerferf erferfre erferf 12545" + 
"\ndsf erf" + 
"\nsdsfd refrf refref" + 
"\nerferf erferfre erferf 12545" + 

And I want to search the number 1234 on 7th Line. It may or may not be present on other lines also.

I have tried with

"\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\d{4}" 

but am not getting the result.

Please help me out with the regular expression.

Upvotes: 5

Views: 861

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213193

Firstly, your newline character should be placed at the end of the lines. That way, picturing a particular line would be easier. Below explanation is based on this modification.

Now, to get to 7th line, you would first need to skip the first 6 line, that you can do with {n,m} quantifier. You don't need to write .*\n 6 times. So, that would be like this:

(.*\n){6}

And then you are at 7th line, where you can match your required digit. That part would be something like this:

.*?1234

And then match rest of the text, using .*

So, your final regex would look like:

(?s)(.*\n){6}.*?1234.*

So, just use String#matches(regex) method with this regex.

P.S. (?s) is used to enable single-line matching. Since dot(.) by default, does not matches the newline character.

To print something you matched, you can use capture groups:

(?s)(?:.*\n){6}.*?(1234).*

This will capture 1234 if matched in group 1. Although it seems unusual, that you capture an exact string that you are matching - like capturing 1234 is no sense here, as you know you are matching 1234, and not against \\d, in which case you might be interested in exactly what are those digits.

Upvotes: 5

Roney Michael
Roney Michael

Reputation: 3994

You can use:

(^.*\r\n)(^.*\r\n)(^.*\r\n)(^.*\r\n)(^.*\r\n)(^.*\r\n)(^.*)(1234)

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try

Pattern p = Pattern.compile("^(\\n.*){6}\\n.*\\d{4}" );
System.out.println(p.matcher(s).find());

Upvotes: 2

MarioDS
MarioDS

Reputation: 13063

This problem is better not solved with regex alone. Start by splitting the string on a newline character, to get an array of lines:

String[] lines = data.split("\\n");

Then, to execute the regex on line 7:

try {
    String line7 = lines[6];
    // do something with it
} catch (IndexOutOfBoundsException ex) {
    System.error.println("Line not found");
}

Hope this is a start for you.

Edit: I'm not a pro in Regex but I would try with this one:

"(\\n.*){5}(.*)"

Sorry if this isn't the correct Java syntax but this should capture 5 new lines + data first, so that's six lines gone, and the data itself should be available in the second capture group (including newline). If you want to exclude the newline in front:

"(\\n.*){5}\\n(.*)"

Upvotes: 1

Related Questions