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(Matcher.java:706)
at java.util.regex.Matcher.replaceAll(Matcher.java:806)
at java.lang.String.replaceAll(String.java:2000)
at net.pascalalma.Test.main(Test.java:14)
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….
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).
nice help!!!thanks a lot for the explanation.