Question

I need to replace all kinds of string that look like {\'\i} by just i

I've already read Java String ReplaceAll method giving illegal repetition error?

So I tried

String word = "something{\\\'\\\i}".replaceAll("\\\\{\\\'\\\i}", "DONE");

but it doesn't work, could anyone help me please?

Was it helpful?

Solution

replaceAll expects regular expressions. Instead of trying to get the RegEx right, use replace.

String word = "something{\\'\\i}".replace("{\\'\\i}", "DONE");

OTHER TIPS

I think you have to use

String word = "yourtexthere"
String newWord = word.replaceAll("{\'\i}", "i");

Try this:

final String a = "text{\\'\\i}";
System.out.println(a);
System.out.println(a.replace("{\\'\\i}", "i"));

You should simply use the replace function instead of replaceAll as the last one expects a regex as argument, Ex:

String word = "something{\\'\\i}".replace("{\\'\\i}", "'i' /* or i */");

\ has to be escaped in java, thats why we have \\

Since replaceAll uses a regex, you need to escape all the relevant characters ({}\), like this

"something{\\'\\i}".replaceAll("\\{\\\\'\\\\i\\}", "i");

The \\\\ is because you're escaping the \ first in a Java String, then in the regular expression to match a literal \.

<script>
    word = "{\'\i}";
    a = word.replace("{"," "");
    a = a.replace("}", "");
    a = a.replace("'", "");

</script>

Just create another rule with the character you want to replace. Not a very good solution but its the most "clean".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top