Peculiar issue with replaceAll method in java - java

I have input as below:
Input: 6jVYY3Xnqt<>:"/\|?*GjznpnRQSb
testInput = testInput.replaceAll("[<>:/\\\"|?*]", "-");
output: 6jVYY3Xnqt----\---GjznpnRQSb
But if I do:
testInput = testInput.replaceAll("[<>:/\"|?*]", "-");
testInput = testInput.replace("\\", "-");
output: 6jVYY3Xnqt--------GjznpnRQSb
Is this a bug in java 7? Why is replaceAll not taking the \ character?

You need to double escape the backslash in your regular expression, once for the string literal and once for the regular expression:
testInput= testInput.replaceAll("[<>:/\\\\\"|?*]", "-");
// ^^^^
// Represents one backslash

Related

How to replace string: \\\" with an empty string

I want to remove \\\" from a string using java. I have tried with the code mentioned below but I could not get the expected result.
str.replaceAll("\\\"","");
input string:
{\"name\":\"keyword\",\"value\":\"\\\"duck''s\\\"\",\"compareVal\":\"contains\"}
expected string:
{\"name\":\"keyword\",\"value\":\"duck''s\",\"compareVal\":\"contains\"}
Use replace():
str = str.replace("\\\\\"", "");
replaceAl() uses regex for its search term (which would require a more complex string literal), but you don't need regex - your search term is plain text.
Note also that java string literals require each of your search characters to be escaped (by a leading backslash).
str.replace("\\\\\"","");
Explanation:
First \ => escaping a '\'
Second \ => escaping a '\'
\" => escaping '"'
Because \ and " are reserved symbols you have to indicate you want to treat them as the symbol they are by escaping with \ before.
public static void main(String s[])
{
String inputString = "\\\"name\\\"";
String outputString = inputString.replace("\\", "").replace("\"","");
System.out.println("Output string is as following :" + outputString);
}

replaceAll not working for below query

After executing the below line the b contains the value "\%AMPAMP\$". I want it to be "&". Please help.
String b = a.replaceAll("\%AMPAMP\$", "&");
String is immutable. See the public String replaceAll(String regex,String replacement):
Returns:
The resulting String
You should do:
a = a.replaceAll("\%AMPAMP\$", "&");
Edit:
After you said that you did save it, you should now notice that replaceAll takes a regex and not a String. You should escape the special characters (Escaping a regex is done by \, but in Java \ is written as \\), or use String#quote:
a = a.replaceAll(Pattern.quote("\%AMPAMP\$"), "&");
You don't really need a regex here. Use String#replace(String search, String replace) method like this:
b = a.replace("%AMPAMP$", "&");
btw String#replaceAll method needs a regex where you need to use double backslash to escape $:
b = a.replaceAll("%AMPAMP\\$", "&");

How to split the string using '^' this special character in java?

I want to split the following string "Good^Evening" i used split option it is not split the value. please help me.
This is what I've been trying:
String Val = "Good^Evening";
String[] valArray = Val.Split("^");
I'm assuming you did something like:
String[] parts = str.split("^");
That doesn't work because the argument to split is actually a regular expression, where ^ has a special meaning. Try this instead:
String[] parts = str.split("\\^");
The \\ is really equivalent to a single \ (the first \ is required as a Java escape sequence in string literals). It is then a special character in regular expressions which means "use the next character literally, don't interpret its special meaning".
The regex you should use is "\^" which you write as "\\^" as a Java String literal; i.e.
String[] parts = "Good^Evening".split("\\^");
The regex needs a '\' escape because the caret character ('^') is a meta-character in the regex language. The 2nd '\' escape is needed because '\' is an escape in a String literal.
try this
String str = "Good^Evening";
String newStr = str.replaceAll("[^]+", "");

How to Replace dot (.) in a string in Java

I have a String called persons.name
I want to replace the DOT . with /*/ i.e my output will be persons/*/name
I tried this code:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
I am getting StringIndexOutOfBoundsException.
How do I replace the dot?
You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
If you want to replace a simple string and you don't need the abilities of regular expressions, you can just use replace, not replaceAll.
replace replaces each matching substring but does not interpret its argument as a regular expression.
str = xpath.replace(".", "/*/");
Use Apache Commons Lang:
String a= "\\*\\";
str = StringUtils.replace(xpath, ".", a);
or with standalone JDK:
String a = "\\*\\"; // or: String a = "/*/";
String replacement = Matcher.quoteReplacement(a);
String searchString = Pattern.quote(".");
String str = xpath.replaceAll(searchString, replacement);
return sentence.replaceAll("\s",".");

How to provide regular expression for matching $$

I am having String str = "$$\\frac{6}{8}$$"; I want to match for strings using starting with '$$' and ending with '$$'
How to write the regular expression for this?
Try using the regex:
^\$\$.*\$\$$
which in Java will be:
^\\$\\$.*\\$\\$$
A $ is a regex metacharacter used as end anchor. To mean a literal $ you need to escape it with a backslash \.
In Java \ is the escape character in a String and also in the regular expression. So to make a \ reach the regex engine you need to have \\ in the String.
See it
Use this regex string:
"^$$.*$$$"
The ^ anchors the expression to the start of the string being matched, and the last $ anchors it to the end. All other $ characters are taken literally.
You may want something like this:
final String str = "$$\\frac{6}{8}$$";
final String latex = "A display math formula " + str + " and once again " + str + " and another one " + "$$42.$$";
final Pattern pattern = Pattern.compile("\\$\\$([^$]|\\$[^$])+\\$\\$");
final Matcher m = pattern.matcher(latex);
while (m.find()) {
System.out.println(m.group());
}

Categories