0

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?

Community
  • 1
  • 1
fatevil
  • 47
  • 1
  • 10

6 Answers6

1

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

String word = "something{\\'\\i}".replace("{\\'\\i}", "DONE");
Cephalopod
  • 14,632
  • 7
  • 51
  • 70
  • ok, you are right, thank you , but if i need to replace them all? it's just a part of the text... so i need to ask if it still contains and "replace" again until it doesnt cotain? – fatevil Aug 13 '13 at 11:16
  • `replace` does replace all occurrences of the text. – Cephalopod Aug 13 '13 at 11:17
0

I think you have to use

String word = "yourtexthere"
String newWord = word.replaceAll("{\'\i}", "i");
Gerret
  • 2,948
  • 4
  • 18
  • 28
  • Don't know what you mean ^^. Use word.replaceAll(%WHATYOUWANTTOREPLCE%, %WHATITSHOULDBE%). If you want all "{" replaced use: word.replaceAll("{", "i") for example. – Gerret Aug 13 '13 at 11:12
0

Try this:

final String a = "text{\\'\\i}";
System.out.println(a);
System.out.println(a.replace("{\\'\\i}", "i"));
r3ap3r
  • 2,775
  • 2
  • 17
  • 16
0

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 \\

M. Abbas
  • 6,409
  • 4
  • 33
  • 46
0

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 \.

Vala
  • 5,628
  • 1
  • 29
  • 55
-1
<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".

Tiago
  • 383
  • 1
  • 7
  • This would replace all sorts of other things, say `{1}`, any other instance of `'`, etc. etc. It's not the most "clean" by any stretch of the imagination, it's just bound to go wrong sooner or later. – Vala Aug 13 '13 at 13:10
  • this will only replace any characters that he wants. Just create a rule for each one...if the initial string is {1} the output will be 1 and that is what is wanted. Or at least i thought so – Tiago Aug 13 '13 at 13:19
  • But he doesn't want to remove all of those characters with no other restrictions, he wants to remove those characters if they're around the `i`. Your code will have loads of side effects under certain conditions; consider for example the (arbitrarily chosen) String `"{x} 'hello' {\'\i}"` - your solution results in `"x hello \\i"`, the desired result is `"{x} 'hello' i"`. Note that your solution doesn't even remove the backslashes. – Vala Aug 13 '13 at 14:45