Recently had to format some messages for HTML display
- by adding "<br>" to plain text where newlines were found.
- If a line already ends with "<br>", then do not add extra "<br>".
- break tags were appearing as "<br>", "<br/>", &quo
t;<br />" and also as "<BR>", "<BR/>", "<BR />
"
- goto be a multi-line match.
- wherever empty lines are that had to be replaced with "<br>"
...
import java.util.regex.Pattern;
...
Pattern pat = Pattern.compile("((?<!\\<(?i)br(?-i)\\s?/?\\s?\\>\\s{0,100})\\n)|(^\\s*$)", Pattern.MULTILINE);
System.out.println(pat.matcher(mystring.trim()).replaceAll("<br>\n")); // Adding a new line to easily spot the newly added "br" tag.
Without \\s{0,100} JVM threw
"java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index ..."