replaceall

Java String ReplaceAll method giving illegal repetition error?

删除回忆录丶 提交于 2019-11-26 14:40:45
问题 I have a string and when I try to run the replaceAll method, I am getting this strange error: String str = "something { } , op"; str = str.replaceAll("o", "\n"); // it works fine str = str.replaceAll("{", "\n"); // does not work and i get a strange error: Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition { How can I replace the occurrences of "{" ? 回答1: A { is a regex meta-character used for range repetitions as {min,max} . To match a literal { you need to

replace \n and \r\n with <br /> in java

妖精的绣舞 提交于 2019-11-26 09:36:59
问题 This has been asked several times for several languages but I can\'t get it to work. I have a string like this String str = \"This is a string.\\nThis is a long string.\"; And I\'m trying to replace the \\n with <br /> using str = str.replaceAll(\"(\\r\\n|\\n)\", \"<br />\"); but the \\n is not getting replaced. I tried to use this RegEx Tool to verify and I see the same result. The input string does not have a match for \"(\\r\\n|\\n)\" . What am i doing wrong ? 回答1: It works for me. public

String replace a Backslash

瘦欲@ 提交于 2019-11-26 06:44:33
问题 How can I do a string replace of a back slash. Input Source String: sSource = \"http://www.example.com\\/value\"; In the above String I want to replace \"\\/\" with a \"/\"; Expected ouput after replace: sSource = \"http://www.example.com/value\"; I get the Source String from a third party, therefore I have control over the format of the String. This is what I have tried Trial 1: sSource.replaceAll(\"\\\\\", \"/\"); Exception Unexpected internal error near index 1 \\ Trial 2: sSource

String.replaceAll single backslashes with double backslashes

北城以北 提交于 2019-11-25 23:40:17
问题 I\'m trying to convert the String \\something\\ into the String \\\\something\\\\ using replaceAll , but I keep getting all kinds of errors. I thought this was the solution: theString.replaceAll(\"\\\\\", \"\\\\\\\\\"); But this gives the below exception: java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 回答1: The String#replaceAll() interprets the argument as a regular expression. The \ is an escape character in both String and regex . You need to double-escape it