replaceall

Java: Replace all ' in a string with \\'

徘徊边缘 提交于 2019-11-30 04:44:24
I need to escape all quotes (') in a string, so it becomes \' I've tried using replaceAll, but it doesn't do anything. For some reason I can't get the regex to work. I'm trying with String s = "You'll be totally awesome, I'm really terrible"; String shouldBecome = "You\'ll be totally awesome, I\'m really terrible"; s = s.replaceAll("'","\\'"); // Doesn't do anything s = s.replaceAll("\'","\\'"); // Doesn't do anything s = s.replaceAll("\\'","\\'"); // Doesn't do anything I'm really stuck here, hope somebody can help me here. Thanks, Iwan Sage You have to first escape the backslash because it's

Matching a whole word with leading or trailing special symbols like dollar in a string

主宰稳场 提交于 2019-11-29 10:30:37
I can replace dollar signs by using Matcher.quoteReplacement. I can replace words by adding boundary characters: from = "\\b" + from + "\\b"; outString = line.replaceAll(from, to); But I can't seem to combine them to replace words with dollar signs. Here's an example. I am trying to replace " $temp4 " (NOT $temp40 ) with " register1 ". String line = "add, $temp4, $temp40, 42"; String to = "register1"; String from = "$temp4"; String outString; from = Matcher.quoteReplacement(from); from = "\\b" + from + "\\b"; //do whole word replacement outString = line.replaceAll(from, to); System.out.println

Java: Replace all ' in a string with \'

女生的网名这么多〃 提交于 2019-11-29 02:38:38
问题 I need to escape all quotes (') in a string, so it becomes \' I've tried using replaceAll, but it doesn't do anything. For some reason I can't get the regex to work. I'm trying with String s = "You'll be totally awesome, I'm really terrible"; String shouldBecome = "You\'ll be totally awesome, I\'m really terrible"; s = s.replaceAll("'","\\'"); // Doesn't do anything s = s.replaceAll("\'","\\'"); // Doesn't do anything s = s.replaceAll("\\'","\\'"); // Doesn't do anything I'm really stuck here

Removing all fraction symbols like “¼” and “½” from a string

浪尽此生 提交于 2019-11-28 20:59:50
I need to modify strings similar to "¼ cups of sugar" to "cups of sugar", meaning replacing all fraction symbols with "". I have referred to this post and managed to remove ¼ using this line: itemName = itemName.replaceAll("\u00BC", ""); but how do I replace every possible fraction symbol out there? Fraction symbols like ¼ and ½ belong to Unicode Category Number, Other [No] . If you are ok with eliminating all 676 characters in that group, you can use the following regular expression: itemName = itemName.replaceAll("\\p{No}+", ""); If not, you can always list them explicitly: // As characters

String replaceAll() vs. Matcher replaceAll() (Performance differences)

本秂侑毒 提交于 2019-11-28 20:04:37
Pretty simple question, but this is coming from a C/C++ person getting into the intricacies of Java. I understand I can fire up jUnit and a few performance tests of my own to get an answer; but I'm just wondering if this is out there. Are there known difference(s) between String.replaceAll() and Matcher.replaceAll() (On a Matcher Object created from a Regex.Pattern) in terms of performance? Also, what are the high-level API 'ish differences between the both? (Immutability, Handling NULLs, Handling empty strings, making coffee etc.) According to the documentation for String.replaceAll , it has

Matching a whole word with leading or trailing special symbols like dollar in a string

て烟熏妆下的殇ゞ 提交于 2019-11-28 03:41:00
问题 I can replace dollar signs by using Matcher.quoteReplacement. I can replace words by adding boundary characters: from = "\\b" + from + "\\b"; outString = line.replaceAll(from, to); But I can't seem to combine them to replace words with dollar signs. Here's an example. I am trying to replace " $temp4 " (NOT $temp40 ) with " register1 ". String line = "add, $temp4, $temp40, 42"; String to = "register1"; String from = "$temp4"; String outString; from = Matcher.quoteReplacement(from); from = "\\b

Regarding Java String Manipulation

橙三吉。 提交于 2019-11-27 14:54:45
I have the string "MO""RET" gets stored in items[1] array after the split command. After it get's stored I do a replaceall on this string and it replaces all the double quotes. But I want it to be stored as MO"RET . How do i do it. In the csv file from which i process using split command Double quotes within the contents of a Text field are repeated (Example: This account is a ""large"" one"). So i want retain the one of the two quotes in the middle of string if it get's repeated and ignore the end quotes if present . How can i do it? String items[] = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$

Removing all fraction symbols like “¼” and “½” from a string

五迷三道 提交于 2019-11-27 13:23:26
问题 I need to modify strings similar to "¼ cups of sugar" to "cups of sugar", meaning replacing all fraction symbols with "". I have referred to this post and managed to remove ¼ using this line: itemName = itemName.replaceAll("\u00BC", ""); but how do I replace every possible fraction symbol out there? 回答1: Fraction symbols like ¼ and ½ belong to Unicode Category Number, Other [No]. If you are ok with eliminating all 676 characters in that group, you can use the following regular expression:

Java String ReplaceAll method giving illegal repetition error?

被刻印的时光 ゝ 提交于 2019-11-27 09:24:14
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 "{" ? A { is a regex meta-character used for range repetitions as {min,max} . To match a literal { you need to escape it by preceding it with a \\ : str = str.replaceAll("\\{", "\n"); // does work fge If you really

String replace a Backslash

笑着哭i 提交于 2019-11-26 17:48:03
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.replaceAll("\\/", "/"); No Exception, but does not do the required replace. Does not do anything. Trial 3: sVideoURL.replace("\\