Java String appending a double quote - java

I want to replace all brackets in a String with the double quote character.
I thought this would work:
"[foo".replaceAll(Pattern.quote("["), Pattern.quote("""));
but it does not. Can anyone help me understand what I need to do?

You need to escape the quotes
"[foo".replaceAll(Pattern.quote("["), "\"");

replaceAll takes strings
"[foo".replaceAll("\\[", "\""));
Might I also add this as a good place to test your regex strings

"[foo".replaceAll(Pattern.quote("["), "\"") ;
The second argument - replacement - is a common string (doesnt need quotation).

"[foo".replaceAll("\\[", "\"")
to escape special characters in strings, like " \, you prepend a \, so \" becomes ", \\ becomes \, etc...

The following works:
"[foo".replaceAll("\[", "\\"")
Notes:
replaceAll interprets its first argument as a regular expression.
you need to escape (within the regex context) the opening bracket or it will be malformed.

Related

Pattern Matching failed when "\" is input

My pattern is something like this:
"^[a-zA-Z0-9_'^&/+-\\.]{1,}#{1,1}[a-zA-Z0-9_'^&/+-.]{1,}$"
But when I try to match something with a backslash in it, like this:
"abc\\#abc"
...it does not match. Can anyone explain why?
try with below pattern
"^[a-zA-Z0-9_'^&/+-\\\\.]{1,}#{1,1}[a-zA-Z0-9_'^&/+-.]{1,}$";
or
"^[a-zA-Z0-9_'^&/+-\\{0,}}.]{1,}#{1,1}[a-zA-Z0-9_'^&/+-.]{1,}$";
The expression \\ matches a single backslash \
Try escaping each backslash of your test string with an additional backslash: e.g.
"abc\\\\#abc" becomes "abc\\\\\\\\#abc"
You need to use "\\\\" if you want the end result to look like "\"
why, you ask?
The Java compiler sees the string "\\\\" and turns that into "\\" as "\" is an escape character.
Afterwards the regular expression sees the string "\\" and turns it into "\" as an "\" is an escape character.
so to want a single backslash you must put in four.
I'm assuming you're writing the regex in your Java source code, like this:
Pattern p = Pattern.compile(
"^[a-zA-Z0-9_'^&/+-\\.]{1,}#{1,1}[a-zA-Z0-9_'^&/+-.]{1,}$"
);
I'm also assuming you meant \\. as a backslash followed by a dot, not as an escaped dot.
Because it's in a string literal, you have to escape backslashes one more time. That means you have to use four backslashes in the regex to match one in the target string. You also need to escape the - (hyphen) so the regex compiler doesn't think (for example) that [+-.] is meant to be a range expression like [0-9] or [a-z].
"^[a-zA-Z0-9_'^&/+\\\\.-]+#[a-zA-Z0-9_'^&/+.-]+$"
I also changed your {1,} to + because it means the same thing, and got rid of the {1,1} because it doesn't do anything. And I changed your & to &. I don't know how that got in there, but if you wrote it that way in your source code, it's wrong.

Android '\' special character

I have an android application where I have to find out if my user entered the special character '\' on a string. But i'm not obtaining success by using the string.replaceAll() method, because Java recognizes \ as the end of the string, instead of the " closing tag. Does anyone have suggestions of how can I fix this?
Here is an example of how I tried to do this:
private void ReplaceSpecial(String text) {
if (text.trim().length() > 0) {
text = text.replaceAll("\", "%5C");
}
It does not work because Java doesn't allow me. Any suggestions?
Try this: You have to use escape character '\'
text = text.replaceAll("\\\\", "%5C");
Try
text = text.replaceAll("\\\\", "%5C");
replaceAll uses regex syntax where \ is special character, so you need to escape it. To do it you need to pass \\ to regex engine but to create string representing regex \\ you need to write it as "\\\\" (\ is also special character in String and requires another escaping for each \)
To avoid this regex mess you can just use replace which is working on literals
text = text.replace("\\", "%5C");
The first parameter to replaceAll is interpreted as a regular expression, so you actually need four backslashes:
text = text.replaceAll("\\\\", "%5C");
four backslashes in a string literal means two backslashes in the actual String, which in turn represents a regular expression that matches a single backslash character.
Alternatively, use replace instead of replaceAll, as recommended by Pshemo, which treats its first argument as a literal string instead of a regex.
text = text.replaceAll("\", "%5C");
Should be:
text = text.replaceAll("\\\\", "%5C");
Why?
Since the backward slash is an escape character. If you want to represent a real backslash, you should use double \ (\\)
Now the first argument of replaceAll is a regular expression. So you need to escape this too! (Which will end up with 4 backslashes).
Alternatively you can use replace which doesn't expect a regex, so you can do:
text = text.replace("\\", "%5C");
First, since "\" is the escape character in Java, you need to use two backslashes to get one backslash. Second, since the replaceAll() method takes a regular expression as a parameter, you will need to escape THAT backslash as well. Thus you need to escape it by using
text = text.replaceAll("\\\\", "%5C");
I could be late but not the least.
Add \\\\ following regex to enable \.
Sample regex:
private val specialCharacters = "-#%\\[\\}+'!/#$^?:;,\\(\"\\)~`.*=&\\{>\\]<_\\\\"
private val PATTERN_SPECIAL_CHARACTER = "^(?=.*[$specialCharacters]).{1,20}$"
Hope it helps.

Removing backslash and newline character (occurring together) in Java

I have stream of data coming from different feeds which I need to clean up.
Data is in specific format and if some sentence spans through multiple lines it is separated using "\"(backslash), which I want to remove. \ is also present in other part of text for escaping quotes etc and I don't want to remove these backslashes. So eventually I want to remove "\\n".
I have tried following regex for removing \ and \n but it didn't work :
singleLine.replaceAll("(\\\\n|\\\\r)", "");
I am not sure what regex would work in this case.
Regex isn't really necessary for this; If I were you, I would use...
singleLine=singleLine.replace("\\\\n", "");
Many people think the replace method only replaces one, but in fact the only difference is that replaceAll uses regex, while replace simply replaces exact matches of the String.
If you do want to use regex though, I believe you have to do \\\\\\\\ (you have to 'nullify' the escape character in Java, and in regex, so x4, not just x2)
Explaining this some more
The only other issue is in your example, you never set singeLine equal to anything; I'm not sure if you hid that, or missed that.
Edit:
Explaining the reasoning for \\\\\\\\ some more, Java requires that you do "\\" to represent one \. Regex also has a use for the \ character, and requires you do the same again for it. If you just "\\" in Java, the regex parser essentially receives "\", it's escape character for certain things. You need to give the regex parser two of them, to escape it, so in Java, you need to do "\\\\" just to represent a match for a single "\"
You'll need 5 backslash characters for each pattern in that regexp.
Use:
singleLine.replaceAll("(\\\\\n|\\\\\r)", "");
The backslash character is both an escape sequence in your string and an escape sequence in the regexp. So to represent a literal \ in a regexp you'll need to use 4 \ characters - your regexp needs \\ to get an escaped backslash, and each of those needs to be escaped in the java String - and then another to represent either \n or \r.
String str = "string with \\\n newline and \\\n newline ...";
String repl = str.replaceAll("(\\\\\n|\\\\\r)", "");
System.out.println("str: " + str);
System.out.println("repl: " + repl);
Output:
STR: string with \
newline and \
newline ...
REPL: string with newline and newline ...
You need to assign the return value to another String object, or the same object, because of String immutability.
singleLine = singleLine.replaceAll("(\\\\n|\\\\r)", "");
More info is here
Remember that Strings are immutable. This means that replaceAll() does not change the String in singleLine. You must use the return value to get the modified String. For example, you can do
singleLine = singleLine.replaceAll("(\\\\n|\\\\r)", "");

JAVA REGEX - Split a string with "?" as delimiter

I want to split a string with "?" as the delimiter.
str.split("?")[0] fails.
The argument to the "split" method must be a regular expression, and the '?' character has special meaning in regular expressions so you have to escape it. That's done by adding a backslash before it in the regexp. However, since the regexp is being supplied by way of a Java String, it requires two backslashes instead so as to get an actual backslash character into the regexp:
str.split( "\\?" )[0];
str.split("\\?")[0]

How to replace a plus character using Java's String.replaceAll method

What's the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java's replaceAll method in the String class? I can't get the syntax right.
You need to escape the + for the regular expression, using \.
However, Java uses a String parameter to construct regular expressions, which uses \ for its own escape sequences. So you have to escape the \ itself:
"\\+"
when in doubt, let java do the work for you:
myStr.replaceAll(Pattern.quote("+"), replaceStr);
You'll need to escape the + with a \ and because \ is itself a special character in Java strings you'll need to escape it with another \.
So your regex string will be defined as "\\+" in Java code.
I.e. this example:
String test = "ABCD+EFGH";
test = test.replaceAll("\\+", "-");
System.out.println(test);
Others have already stated the correct method of:
Escaping the + as \\+
Using the Pattern.quote method which escapes all the regex meta-characters.
Another method that you can use is to put the + in a character class. Many of the regex meta characters (., *, + among many others) are treated literally in the character class.
So you can also do:
orgStr.replaceAll("[+]",replaceStr);
Ideone Link
If you want a simple string find-and-replace (i.e. you don't need regex), it may be simpler to use the StringUtils from Apache Commons, which would allow you to write:
mystr = StringUtils.replace(mystr, "+", "plus");
Say you want to replace - with \\\-, use:
text.replaceAll("-", "\\\\\\\\-");
String str="Hello+Hello";
str=str.replaceAll("\\+","-");
System.out.println(str);
OR
String str="Hello+Hello";
str=str.replace(Pattern.quote(str),"_");
System.out.println(str);
How about replacing multiple ‘+’ with an undefined amount of repeats?
Example: test+test+test+1234
(+) or [+] seem to pick on a single literal character but on repeats.

Categories