java - error in replaceAll with escp -
i using open source ocde
private string undoescape(string searchterm) { string specialchars = "+-&|!(){}[]^\"~:\\"; (int = 0; < specialchars.length(); i++) { string character = specialchars.substring(i, + 1); string escaped = "\\" + character; searchterm = searchterm.replaceall(pattern.quote(escaped), character); } return searchterm; }
in case character = \ in inspect = \
escaped = \\ - in inspect = \
searchterm \\n = in inspect = \n
i got error wrong ?
java.lang.stringindexoutofboundsexception: string index out of range: 1 @ java.lang.string.charat(string.java:658) @ java.util.regex.matcher.appendreplacement(matcher.java:762) @ java.util.regex.matcher.replaceall(matcher.java:906) @ java.lang.string.replaceall(string.java:2162) @ org
switch replace()
, replaces all occurrences doesn't require regex:
private string unescape(string searchterm) { string specialchars = "+-&|!(){}[]^\"~:\\"; (char c : specialchars.tochararray()) { string character = string.valueof(c); string escaped = "\\" + character; searchterm = searchterm.replace(escaped, character); } return searchterm; }
Comments
Post a Comment