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);
}
Related
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
I need to delete all tokens that are started with \ and followed by any characters.
I created such a pattern:
input.replaceAll("\\[a-zA-Z0-9]*", "");
But it doesn't work because it doesn't delete \rad from string 5 4\rad.
EDIT:
public static void main(String[] args)
{
String input="Wolf 3 3\4par";
String replaceAll = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
System.out.println("replaceAll=" + replaceAll);
}
Thank you!
The \ is special both in string literals and in regular expressions. To put an actual \ in a regular expression, you have to escape it twice. You also have to assign the result somewhere, which it wasn't clear from your question you were doing. So:
input = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
Complete example: Live Copy
import java.util.*;
public class Temp {
public static void main(String[] args) {
String input = "4 5 \\rad";
input = input.replaceAll("\\\\[a-zA-Z0-9]*", "");
System.out.println(input);
}
}
Output:
4 5
To create \ literal in regex you need to pass \\ to regex engine. But to create \ literal in String you also have to escape it so you need to write it as "\\".
\ literal in regex engine
\\ regex pattern
"\\\\" String representing regex pattern
Now you are using one \ in your regex pattern regex engine sees it as \[ which escapes [ making it simple literal.
Try this way
input.replaceAll("\\\\[a-zA-Z0-9]*", "");
From
Sorry, but my String is exactly 5 4\rad. Indeed how to delete \rad? – Volodymyr Levytskyi
Try
String k= "5 4\rad";
System.out.println(k.replaceAll("\r\\w*", ""));
Output
5 4
I'm getting message from other program where some characters are changed:
\n (enter) -> #
(hash) # -> \#
\ -> \\\\
When I'm trying to reverse these change with my code it's not working, probably of that
Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
This is my code:
public String changeChars(final String message) {
String changedMessage = message;
changedMessage = changePattern(changedMessage, "[^\\\\][#]", "\n");
changedMessage = changePattern(changedMessage, "([^\\\\][\\\\#])", "#");
changedMessage = changePattern(changedMessage, "[\\\\\\\\\\\\\\\\]", "\\\\");
return changedMessage;
}
private String changePattern(final String message, String patternString, String replaceString) {
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(message);
return matcher.replaceAll(replaceString);
}
I assume that your encoding method works like this.
replace all \ with \\\\
mark originally placed # as \#
now since we know that all originally placed # have \ before it we can use it to mark new lines \n with #.
Code for that could be something like
data = data.replace("\\", "\\\\\\\\");
data = data.replace("#", "\\#");
data = data.replace("\n", "#");
To reverse this operation we need to start from the end (form last replacement)
We will replace all # that don't have \ before it with new line \n marks (if we started with 2nd replacement \# -> # we wouldn't know later which of # ware replacements of \n).
After that we can safely replace \# with # (this way we will get rid of additional \ that wasn't in original String and it won't bother our last replacement step).
and lastly we replace \\\\ with \.
Here is how we can do it.
//your previous regex [^\\\\][#] describes "any character that is not \ and #
//but since we don't want to include that additional non `\` mark while replacing
//we should use negative look-behind mechanism "(?<!prefix)"
data = data.replaceAll("(?<!\\\\)#", "\n");
//now since we got rid of additional "#" its time to replace `\#` to `#`
data = data.replace("\\#", "#");
//and lastly `\\\\` to `\`
data = data.replace("\\\\\\\\", "\\");
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("[^]+", "");
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());
}