This question already has answers here:
How to split a java string at backslash
(8 answers)
Closed 7 years ago.
I have the following string:
c:\Users\moises\file
and I'm trying to use:
String fileName = path.split("\\")[3];
but I get this error:
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
How can I split the string with the "\" character
path.split("\\\\");
You need to escape your escapes
You'll need four backslashes in a row.
The regular expression with then consist of two backslashes: that means a literal backslash.
I think, it is because you need to escape four times, try this:
path.split("\\\\");
Related
This question already has answers here:
Trim leading or trailing characters from a string?
(6 answers)
Closed 2 years ago.
I'd like to trim any single trailing . or -. I tried doing this by doing something like "f-o.o.".replaceFirst("^(\\.+)[-|.]$", "$0"). The expected string is f-o.o but I'm getting f-o.o.. Thank you.
Your expression has two mistakes:
you put a slash in front of a dot, making it match a literal dot, not just any character
you put | into a character class, so your expression would remove not just . or - at the end of the string, but also |.
Use "f-o.o.".replaceFirst("[-.]$", "")
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("\\*", " ");
This question already has answers here:
Why does this Java regex cause "illegal escape character" errors?
(7 answers)
Closed 3 years ago.
I want to split a string in java with white spaces. I know that the below line of code does it.
String s[] = str.split("\\\s+");
Here split function takes the regex by which the string must be split. So when I want to split string str through one or more spaces, I should pass \s+ as regex, then why is \\\s+ used?
This will do the split
String s[] = n.split("\\s+");
You don't need a third slash'\' - you get Compile Error.
And first '\' is for escaping the second '\'. Used as an escape character for '\s'.
Like Ismail said, you don't need the third backslash.
In your regex you want to use \s so in Java you also need to escape your backslashes for your tags.
Solution:
Why does this Java regex cause "illegal escape character" errors?
This question already has answers here:
Java doesn't work with regex \s, says: invalid escape sequence
(3 answers)
Closed 5 years ago.
I am trying do that but it keeps give me error invalid escape format. I was trying to remove some backslash but didn't work
private string chars = "(lP+" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?($|-|[\\w\\\\\.#]+)\"?")";
String should have a capital 'S'
You've missed esacaping a couple of the quotation marks.
You have a run of five backslashes, which should either be 4 or 6.
You probably want something like this instead...
private String chars = "(lP+\" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?($|-|[\\w\\\\\\.#]+)\"?\")";
private String chars = "(lP+\" (\\w+)/\\d{3} \\d+ \\w+ \\S+\\s?\"?(\\$|-|[\\w\\\\.#]+)\"?\")";
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.