Java regex capture EOL, regex over stream -
i'm trying implement regex on stream (reader). i'm using following approach:
- read n chars buffer
- call matcher.find() on buffer
- trim beginning till matcher.end() position , append buffer n chars length
- 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
Post a Comment