Solutions to IT problems

Solutions I found when learning new IT stuff

Java Command-Line Applications and Regular Expression Arguments

leave a comment »


Introduction

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");
Advertisements

Written by kienerj

May 7, 2013 at 14:23

Posted in Java, Programming

Tagged with ,

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: