0

How to get text inbetween Digits and Blankline Using Java I Used Regex which shows

            Exception in thread "main" java.lang.StringIndexOutOfBoundsException: 
            String index out of range: -1

Code Here:

            List<String> file = Files.readAllLines(Paths.get(path), 
            StandardCharsets.UTF_8);
               for(String v:file)
               {
                  if(v!=null)
                  {
                     String answer = v.substring
                     (v.indexOf("//d")+1,v.indexOf("\\n"));
                    System.out.println(answer);
                  }
               }

What is wrong with my regex?

      My File  :text.txt
                    fruits
                  1 Mango
                    Apple
                    Blank line
                    Veg
                  2 Onion
                    Tomato
                    Blank line

My Excepted Output is :

                    Mango
                    Apple
                    Onion
                    Tomato

But I Got Exception

Note : This File is Sample.And original File is Too Long
I mean blank line not new line

hesh
  • 15
  • 4
  • I think perhaps something's wrong with you `v.indexOf("\\n")` – Scratte Jun 05 '20 at 12:13
  • Why do you expect `Apple` and `Tomato`? They seem to be on separate lines with no digits. – Scratte Jun 05 '20 at 12:14
  • that comment i used to get upto first index of newline after number – hesh Jun 05 '20 at 12:14
  • `"//d"` should be `"\\d"` – Toto Jun 05 '20 at 12:15
  • yes but apple and tomato are inbetween number and blank line as i want to get text inbetween digits and blank line – hesh Jun 05 '20 at 12:16
  • So.. in your file, there are 8 separate lines? And you want to print everything between a number and a blank line? Not the end of a line? (There is usually a newline at the end of every line in a file) – Scratte Jun 05 '20 at 12:16
  • 1
    my file is too long. i just posted my file sample..and yes i want to print everything in between number and blank line and not end of line – hesh Jun 05 '20 at 12:19
  • Sorry, I meant you should write in your Question that you do not mean newline, you mean the blank line :) – Scratte Jun 05 '20 at 20:17

1 Answers1

0

You can use the below regex to achieve your purpose:

(\d+ )([\w\W]+?)(?=((\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^\s*$)

Explanation of the above regex:

(\d+ ) - Represents first capturing group matching 1 or more digits followed by a space character. If space characters are more than one then you can use the + quantifier.

([\w\W]+) - Represents second capturing group matching every character and non-character classes.

(?=) - Represents positive look-ahead asserting any empty lines appearing in front.

((\r\n|\n|\r)$)|(^(\r\n|\n|\r))|^\s*$ - Matches any empty line present in the test String. For better insights; please read this answer.

Pictorial representation

You can find the demo of the above regex in here.

Implementation in java:

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

class Main {
  private static final Pattern pattern = Pattern.compile("(?:\\d+ )([\\w\\W]+?)(?=(?:(?:\\r\\n|\\n|\\r)$)|(?:^(?:\\r\\n|\\n|\\r))|^\\s*$)", Pattern.MULTILINE);

  public static void main(String[] args) throws IOException {
    Path path = FileSystems.getDefault().getPath("folder", "data.txt");
    String content = new String(Files.readAllBytes(path));
    StringBuilder sb = new StringBuilder();
    Matcher matcher = pattern.matcher(content);

    while (matcher.find()) {
      sb.append(matcher.group(1) + "\n"); \\ \n to separate every match in a newline.
    }
    System.out.println(sb.toString());
  }
}

You can find the sample run of the above implementation in here.

Community
  • 1
  • 1
  • thanks for ur time..i am sorry i mean blank line and not the new line.for me this code runs but empty console as you used the space regex(i thing) – hesh Jun 06 '20 at 05:51
  • but your data.txt file does not have any empty line.i mean blank line – hesh Jun 06 '20 at 06:03
  • that was my falut i wrongly mentioned new line instead of blank line – hesh Jun 06 '20 at 06:04