I am looking for Java solution to replace line breaks with <br/>
tags in all multi-line text fields in a given HTML string, that are not enclosed in any tags (children of an imaginary root).
The source data is an HTML-formatted text created via front-end HTML editor (like TinyMCE). So it's an arbitrary HTML fragment - a part of a non-existing <body>
.
The following:
text11
text 21<p>tagged text1
tagged text2</p>
text 2
Should become:
text11<br/>text 21<p>tagged text1
tagged text2</p></br>text 2
The following, however, should not be impacted at all:
<div>text11
text 21<p>tagged text1
tagged text2</p>
text 2</div>
I was thinking about something like this (not working):
private static String ReplaceLfWithBr(String source) {
// text - combination of words and line breaks
// should not be preceded by <tag> or followed by <\tag>
final String regex = "((?!<.+>)[\\w(\\r?\\n)]+(?!<\\s*/.+>))";
Pattern patern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = patern.matcher(source);
StringBuffer sb = new StringBuffer(source.length());
while(matcher.find()){
matcher.appendReplacement(sb, "<br/>");
}
matcher.appendTail(sb);
return sb.toString();
}
– Hard Tacos Oct 08 '15 at 15:38
` replace on that work? – gla3dr Oct 08 '15 at 15:41