groovy: how to replaceAll ‘)’ with ‘ ‘
Same as in Java: def str2 = str1.replaceAll(‘\\)’,’ ‘) You have to escape the backslash (with another backslash).
Same as in Java: def str2 = str1.replaceAll(‘\\)’,’ ‘) You have to escape the backslash (with another backslash).
From gleaning the documentation for replaceAll, we find the following tidbits: const newStr = str.replaceAll(regexp|substr, newSubstr|function) Note: When using a regexp you have to set the global (“g”) flag; otherwise, it will throw a TypeError: “replaceAll must be called with a global RegExp”. In other words, when calling replaceAll with a regex literal or RegExp, … Read more
sSource = sSource.replace(“\\/”, “https://stackoverflow.com/”); String is immutable – each method you invoke on it does not change its state. It returns a new instance holding the new state instead. So you have to assign the new value to a variable (it can be the same variable) replaceAll(..) uses regex. You don’t need that.
You need to use the Pattern.DOTALL flag to say that the dot should match newlines. e.g. Pattern.compile(regex, Pattern.DOTALL).matcher(testIllegal).replaceAll(“x”) or alternatively specify the flag in the pattern using (?s) e.g. String regex = “(?s)\\s*/\\*.*\\*/”;
use replaceAll and toLowerCase methods like this: myString = myString.replaceAll(” “, “_”).toLowerCase()
According to the documentation for String.replaceAll, it has the following to say about calling the method: An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression Pattern.compile(regex).matcher(str).replaceAll(repl) Therefore, it can be expected the performance between invoking the String.replaceAll, and explicitly creating a Matcher and Pattern should be … Read more
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 (requires UTF-8 source file encoding) itemName = itemName.replaceAll(“[¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞↉]+”, … Read more
It works for me. public class Program { public static void main(String[] args) { String str = “This is a string.\nThis is a long string.”; str = str.replaceAll(“(\r\n|\n)”, “<br />”); System.out.println(str); } } Result: This is a string.<br />This is a long string. Your problem is somewhere else.
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 for regex: string.replaceAll(“\\\\”, “\\\\\\\\”); But you don’t necessarily need regex for this, simply because you want an exact character-by-character replacement and you don’t need patterns here. So String#replace() should suffice: … Read more