Thursday, July 18, 2013

Multi-line match in Java RegEx

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 ..."

Tomcat7 Registry Settings

Needed to adjust the heap settings for Tomcat running as a service, but tomcat7w.exe did not launch on a Windows 2008 server, so had to dig out the registry settings.

Right click to open this image in a separate window to see the complete image.