Solutions to IT problems

Solutions I found when learning new IT stuff

Posts Tagged ‘cli

Java Command-Line Applications and Regular Expression Arguments

leave a comment »


I needed to create a simple tool to split pdfs into multiple documents where the splitting is defined by a certain text appearing on a page. Or said otherwise the occurrence of the specific text (regex) marks the start of a new document.

The issue

The application seemed to work correctly…until I actually used it from the command-line. It was obvious something is wrong with the regex and in fact I realized that newline character \n was an actual backslash + n in the string or "\\n" written as a Java String. I googled and found that this expected behavior and it does make sense. The solution is simple. Just replace it with an actual newline:

String regex = args[1];
regex = regex.replace("\\r", "\r"); // for Windows
regex = regex.replace("\\n", "\n");

Written by kienerj

May 7, 2013 at 14:23

Posted in Java, Programming

Tagged with ,