Wednesday, 15 February 2012

java - StringIndexOutOfBoundsException on replaceAll using lookahead and backslash? -


I want to avoid all quotes (single and double) and backslash of string.

I am trying to do this through a call to my string. To match and replace, regex is (? = ['\ "\\\\]) (java escaped syntax), or more readable: (? = ['! \ ]) (Unscade syntax). I am doing a letterhead, because I want to place those quotes and backslashes, and just put an escape character before each of them.

If I + As an escape character, everything works like a magic (Java syntax below all strings Avoid):

"abc'def \" ghi \\ jkl ".replaceAll (" (? = ['\ "\\\\])", "+") result in "abc + 'def + \" ghi + \\ jkl ". Yes!

However, if I use a backslash as escape character, then I am getting StringIndexOutOfBoundsException instead: outside the string index range: 1 .

The call looks like this: "abc'def \" ghi \\ jkl ".replaceAll (" (? = ['\ "\\\\])", "\\\" \ ")

It is weird ... does the inserted backslash interfere in the backslash somehow behind the interference? If so, how can I avoid this behavior?

Here is a complete test code:

  Import static OrganGet.नेट Asset. Import static org.junit.assert.assertFalse; Import static org.junit.Assert.assertTrue; Import org.junit.Test; Public class reggaestest {@Test Public Zero Test () {assertEquals ("ABC \\ 'DEF \\\" ghi \\\\ jkl "," abc'def \ "ghhi \\ jkl" .replaceAll ("(? [? [ '\ "\\\\]", "\\\" ");}}  

Any help is appreciated!

< P>

No comments:

Post a Comment