Replace special character with output preceding special character in Java

In my java code, if any of the special characters were added to the string input, which should be preceded by \\

Special character set {+, -, &&, ||, !, (, ), {, },[, ], ^, "", ~, *, ?, :, \} . I tried using String.replaceAll(old,new) , but, to my surprise, it does not work, although I give the correct values ​​for the "old" and "new".

 if old=":",new="\:" 

I put special characters in the String array, repeated it in a for loop, checked if it was present in the string, if so, input.replaceAll(":","\\:") . But this does not give me the intended exit. Please, help

 String[] arr = { "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", "\\", "AND", "OR" }; for (int i = 0; i < arr.length; i++) { //'search' is my input string if (search.contains((String) arr[i])) { String oldString = (String) arr[i]; String newString = new String("\\" + arr[i]); search = search.replaceAll(oldString, newString); String newSearch = new String(search.replaceAll(arr[i], newString)); } } 
+4
source share
4 answers

Once you understand that replaceAll accepts a regular expression, it's just a matter of encoding your characters as a regular expression.

Try the following:

 String newSearch = search.replaceAll("(?=[]\\[+&|!(){}^\"~*?:\\\\-])", "\\\\"); 

This is a whacky regex - this is a "look ahead" - a non-committing statement that the next char something matches - in this case a character class.

Please note that you do not need to avoid characters in the character class except ] (even minus does not require escaping if the first or last).

\\\\ is how you code the regular expression literal \ (runs once for java, once for regular expression)


Here is a test of this work:

 public static void main(String[] args) { String search = "code:xy"; String newSearch = search.replaceAll("(?=[]\\[+&|!(){}^\"~*?:\\\\-])", "\\\\"); System.out.println(newSearch); } 

Output:

 code\:xy 
+8
source

String#replaceAll takes regex as the first parameter. This way your code will fail if the input in the line is meta-character like - + .

You must use String#replace .

And also you do not need the last appointment: -

 String newSearch = new String(search.replaceAll(arr[i], newString)); 

Like, you do not use it at all. In fact, you assign the changed string to search , so it is not required.

Also, instead of using new String(...) to create a new line. In fact, you just need one line in the if-statement .

Now, after this explanation , you can use below for-loop : -

 for (int i = 0; i < arr.length; i++) { if (search.contains(arr[i])) { search = search.replace(arr[i], "\\" + arr[i]); } } 
+3
source

Try using the below. Use replacement method instead of ReplaceAll

search = search.replace (oldString, newString);

+2
source

when i tried the code below it worked

 String[] arr = { "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":", "\\", "AND", "OR" }; for (int i = 0; i < arr.length; i++) { //'search' is my input string if (search.contains((String) arr[i])) { String oldString = (String) arr[i]; String newString = new String("\\" + arr[i]); search = search.replaceAll(oldString,(String) ("\\" + newString)); } } 
0
source

All Articles