Java regex capture EOL, regex over stream -


i'm trying implement regex on stream (reader). i'm using following approach:

  1. read n chars buffer
  2. call matcher.find() on buffer
  3. trim beginning till matcher.end() position , append buffer n chars length
  4. loop

there issue regex ^(.*)$ (it user data , not possible change it). matcher did not capture line endings, matcher.end() returns last symbol before \n char not \n itself. , after program loops in empty string. e.g. have input "abc\ncde"

  • matcher captures abc , matcher.end() returns 2
  • substring(2) returns "\ncde"
  • matcher captures empty string , loops here

is there way make mather include eol chars capturing group? or there regex implementations java work streams?

p.s. there 1 solution ugly, not wont implement it:

string text = "abc\r\ndef\r\nghi\r\n"; pattern pattern = pattern.compile("^(.*)$", pattern.multiline); boolean first = true; matcher matcher = pattern.matcher(text); while (matcher.find(first ? 0 : 1)) {     system.out.println(matcher.group());     text = text.substring(matcher.end() - 1);     matcher.reset(text);     first = false; } 


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -