Java, Regular Expression HasNext starts with empty line, multi-platform support

穿精又带淫゛_ 提交于 2019-12-24 06:41:14

问题


I need to process the following file on Unix and Windows:

a;b
c;d;e;f;g
c;d;e;f;g
c;d;e;f;g

a;b
c;d;e;f;g
c;d;e;f;g
c;d;e;f;g

a;b

a;b
c;d;e;f;g
c;d;e;f;g
c;d;e;f;g

i need to process a;b that contain a block of data underneath. e.g. the third a;b shouldn't be processed.

currently i am delimiting by using the following regular expression this type of text in a file using Java scanner:

Scanner fileScanner = new Scanner(file);
        try{

            fileScanner.useDelimiter(Pattern.compile("^$", Pattern.MULTILINE));

            while(fileScanner.hasNext()){
                String line;
                while ((line = fileScanner.nextLine()).isEmpty());
                InputStream is = new ByteArrayInputStream(fileScanner.next().getBytes("UTF-8"));
...

This will still delegate for the third a;b the empty input into the ByteArrayInputStream.

Hoe may i check if the first line of fileScanner.next() is an empty line and then execute nextLine() statement and a following a continue statement?


回答1:


Use regex pattern

(?m)^(?:.+(?:\\r?\\n|\\Z)){2,}

which matches two or more non-empty lines, or other words two or more (?:...){2,} lines that contain one or more characters .+ followed by new line \\r?\\n or (?:...|...) end of string \\Z.

Multiline modifier (?m) means that ^ matches a beginning of each line, not just the beginning of the string.


Demo:

String str = "...";

Pattern p = Pattern.compile("(?m)^(?:.+(?:\\r?\\n|\\Z)){2,}");
Matcher m = p.matcher(str);
while (m.find()) {
  String match = m.group();
  System.out.println(match);
}

See this demo.



来源:https://stackoverflow.com/questions/13232689/java-regular-expression-hasnext-starts-with-empty-line-multi-platform-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!