Regex syntax drama in Java

Last time, I had to create a quick fix in a rather ugly Java program. The functionality that had to be added was replacing the characters ‘||’ with the characters ‘|$|’.
So I made something like this:

myString = myString.replaceAll("||","|$|");

Now that was a quick fix, deploy it and move on…. NOT. The code I added just gave me this exception:

Exception in thread “main” java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(
at java.util.regex.Matcher.replaceAll(
at java.lang.String.replaceAll(
at net.pascalalma.Test.main(

It appears that the replaceAll function is using regex to find and replace the Strings and the ‘|’ character has a special function in regex. Okay, so I’ll just escape the character:

myString = myString.replaceAll("\|\|","\|$\|");

Well, that is even worse. It is now complaining at compile time:
Error(14,38): errorInvalidEscapeChar
And I was in a big hurry….

So I decided to do the “||” replacement on the textfile, which was used as input, with a text editor, so I could at least move on.

And of course, lateron, just after a quick google, you’ll find that you just had to double escape the regex characters, like this, to make it work:

 myString = myString.replaceAll("\\|\\|","\\|\\$\\|");

Although it does no good to the readiblity (and thus maintainability) of the code….

About Pascal Alma

Pascal is a senior software developer and architect. Pascal has been designing and building applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Lately he is having great fun by building iOS apps with Swift. Specialties: JEE AWS XML/XSD/XSLT Web Services/SOA Mule ESB/ WSO2 ESB Maven Cloud Technology Swift/ iOS
This entry was posted in Technical. Bookmark the permalink.

2 Responses to Regex syntax drama in Java

  1. Rob Augustinus says:

    That’s an inherent problem with regular expressions. They become quite unreadable, but
    they are oh so powerfull. I am currently working with the following regex : “\$[a-zA-z[.]]”
    which basically pulls out all $objectname.getter.maybeanothergetter like strings which will
    be replaced with some values provided by some reflection code.

    Question remains, how can I tell the end of the string to replace if it doesn’t end with
    a special character (like a space or another delimiter?:-) Otoh this is not a problem (yet),
    and a possible solution would be to specify some other character to denote the end
    of the string (maybe some brackets).

  2. pandu says:

    nice help!!!thanks a lot for the explanation.

Comments are closed.