This question already has answers here:
Java doesn't work with regex \s, says: invalid escape sequence
(3 answers)
Closed 3 years ago.
I have a string
String a = "dcvdk*vmfdkvm*bmkjfnb*";
I want to replace the * character by space
I tried a.replaceAll("\*", " ");
But it is giving error as invalid escape sequence.
Can you please tell me how can I achieve this?
Escape the escape character:
"\\*"
Alternatively, just use replace, which treats the arguments as literals, not regexes:
a.replace("*", " ")
Or, as Aniket Sahrawat points out, you can use the char overload in this case:
a.replace('*', ' ')
Remember that the backslash have a special meaning in strings, and you need to escape the backslash itself to get an actual backslash:
a.replaceAll("\\*", " ");
Related
This question already has answers here:
Java replaceAll: Cannot replace string with backslash
(3 answers)
What are all the escape characters?
(5 answers)
Closed 11 months ago.
I have a string like s = "abc\def" and I want to replace "" with "/" and makes the string like "abc/def/".
I tried replaceAll("\\","/") but the compiler is giving error for string
error: illegal escape character
String s="abc\def";
The issue is that Java uses \ as the string escape character, but also as the regex escape character, and replaceAll performs a regex search. So you need to doubly escape the backslash (once to make it a literal \ in the string, and once to make it a literal character in the regular expression):
result = str.replaceAll("\\\\", "/");
Alternatively, you don’t actually need regular expressions here, so the following works as well:
result = str.replace("\\", "/");
Or, indeed, using the single-char replacement version:
result = str.replace('\\', '/');
This question already has answers here:
Escaping special characters in Java Regular Expressions
(7 answers)
Closed 6 years ago.
I want to do what the title says , although i know how it can be done using the code below:
(Where local is a variable which represents a path):
String path = ( local.startsWith(File.separator) || local.startsWith("/") || local.startsWith("\\"))
? local.substring(File.separator.length(), local.length())
: local;
I need to convert the above to regex expression so i am using:
path = local.replaceFirst("[" + File.separator + "/\\]", "");
Coming from xml schema where i was using regex expressions and looking on tutorials here it seems to me that it must work but it doens't at all, i get this error ->:
java.util.regex.PatternSyntaxException: Unclosed character class near index 4
[\/\]
^
If i change the code to the below,it works:
local.replaceFirst("[" + File.separator + "/\\Q \\ \\E]", "");
Here is saying that \ is a special character but:
There are two ways to force a metacharacter to be treated as an ordinary character:
precede the metacharacter with a backslash, or
enclose it within \Q (which starts the quote) and \E (which ends it).
This question was also read : Forward slash in Java Regex
Well the error was :
By simply using \\ it was replaced by \ so i had only the special character.
The solution is to use \\\\ so it is replaced by \\ and the first \ treats the second \ as a character and not a special character.
Leaving this solution here in case somebody has the same problem..
This question already has answers here:
What is the backslash character (\\)?
(6 answers)
Closed 7 years ago.
Why does this string print only ""\\""? Does the backslash do something to the string? Please explain the function of the backslash. All I know is that it is the escape character, but I don't understand why it does this to strings.
The backslash '\' can be used in a String to add characters that would otherwise be illegal (e.g. " and ') or have another meaning (e.g. t, b, n, r, f and \). for your particular example :
The first 2 backslashes are escaping the double quotes. So \"\" is printed as ""
The next backslashes are escaping the backslashes that immediately follow so \\\\ is printed as \\
The last 2 backslashes behave as the first 2 escaping the quotes so \"\" is printed as ""
The Backslash is the escape character, used to encode special things like " in your string (which you normally couldn't use, because they'd mark the end of a string). You should read up on "String literals" in the official Java documentation or the book you read to learn Java.
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 9 years ago.
I'm trying to split a string at every '.' (period), but periods are a symbol used by java regexes. Example code,
String outstr = "Apis dubli hre. Agro duli demmos,".split(".");
I can't escape the period character, so how else do I get Java to ignore it?
Use "\\." instead. Just using . means 'any character'.
I can't escape the period character, so how else do I get Java to ignore it?
You can escape the period character, but you must first consider how the string is interpreted.
In a Java string (that is fed to Pattern.compile(s))...
"." is a regex meaning any character.
"\." is an illegally-escaped string. This won't compile. As a regex in a text editor, however, this is perfectly legitimate, and means a literal dot.
"\\." is a Java string that, once interpreted, becomes the regular expression \., which is again the escaped (literal) dot.
What you want is
String outstr = "Apis dubli hre. Agro duli demmos,".split("\\.");
This question already has answers here:
Removing a substring between two characters (java)
(3 answers)
Closed 9 years ago.
I want to remove a string that is between two characters and also the characters itself , lets say for example:
i want to replace all the occurrence of the string between "#?" and ";" and remove it with the characters.
From this
"this #?anystring; is #?anystring2jk; test"
To This
"this is test"
how could i do it in java ?
#computerish your answer executes with errors in Java. The modified version works.
myString.replaceAll("#\\?.*?;", "");
The reason being the ? should be escaped by 2 backslashes else the JVM compiler throws a runtime error illegal escape character. You escape ? characters using the backslash .However, the backslash character() is itself a special character, so you need to escape it as well with another backslash.
Use regex:
myString.replaceAll("#\?.*?;", "");
string.replaceAll(start+".*"+end, "")
is the easy starting point. You might have to deal with greediness of the regex operators, however.