regex - Dynamically inserting characters into a StringBuilder and Java Matcher -
i have following scenario:
i have 1 liner flat file. line structured such has a header , corresponding data. looks this:
header1 data data data data data header2 data data header3 data header4 data ....
i have convert 1 liner format, each header on separate line, along data. so, should this:
header1 data data data data data header2 data data header3 data
the "header" follows consistent pattern in length , type of characters use. so, figured java regex pattern
, matcher
way go.
i using stringbuilder
, since has insert()
method, using insert line separator.
the problem having there line @ end of newly created file (the 1 line separator inserts) consists of several headers i.e don't seem broken new lines. seems reason fact matcher.find()
stumbles upon match has start index outside of matcher's region
execution exits code new line inserted.
this behavior inconsistent. have flat files short (about 50 lines), problem not appear. have flat files 20k bytes/characters, problem appears.
it seems matcher
matcher.find()
goes of initial data (region) supplied when reading 1 liner. let's matcher region 0 19688
. but, inserting system.lineseparator()
size of stringbuilder
dynamically increases 2 bytes (\r\n)
i have tried using matcher.reset()
or modifying matcher's region
suggested here: replace text in stringbuilder via regex
how deal issue in efficient , correct way? thanks
p.s. regex not problem. regex matches every single header have in 1 liner. thought i'd point out avoid discussing regex itself.
here code:
bufferedreader br = new bufferedreader(new filereader(constants.source_location+filename)); try { string origline = br.readline(); stringbuilder line = null; while (origline != null) { line = new stringbuilder(origline); pattern pattern = pattern.compile(constants.al3group_regex_pattern); matcher matcher = pattern.matcher(line); while (matcher.find()) { line.insert(matcher.start(), system.lineseparator()); } origline = br.readline(); } converterfilecontents = line.tostring(); printwriter writer = new printwriter("sample\\output.txt"); writer.println(converterfilecontents); writer.close(); system.out.println(converterfilecontents); } { br.close(); }
try replaceall
str = str.replaceall(" (header\\d+)", "\r\n$1");
Comments
Post a Comment