I want to ask something about java regex and hope I will find some help here.
I have a string which looks like "textX|textY|textZ".
Now I want to split this on each | character in java.
I tried it with
string.split("\|");
but I just get a "Invalid escape sequence" error.
How can I split the above string when I use | as separator?
You need double escaping in Java so use:
String[] tokens = string.split("\\|")
OR else use character class:
String[] tokens = string.split("[|]");
you should use \\| for escaping meta character.
In Java you need to escape characters twice for regex: once for the string, once for the regex.
Try
string.split("\\|")
You have to use double back slash \\: string.split("\\|") .
It is because back slash is a escape character for both: java and regex. First one escapses the second for java, so that the "real" backslash is passed to regex.
simply doing
string.split("\\|");
in java when we escape we do it by two backslashes
Related
I want to convert all the occurences of \" in a text file into empty string.
So basically I want to convert to .
I used the following method but it doesnt seem to work:
sb.toString().replaceAll("\\"", "");
Can anyone help me with this?
sb.toString().replaceAll(Pattern.quote("\\""), "");
How about instead of replaceAll which uses regex, use simple replace which will automatically escape all regex metacharacters (like in your case "\\") in pattern you want to replace.
String replaced = sb.toString().replace("\\"", "");
Your problem is that in a regular expression, the \ character has a special meaning. You need to escape it with a second \. Then both \ characters need to be escaped from the Java compiler. You actually need to write
sb.toString().replaceAll("\\\\"", "");
I have a String representing a directory, where \ is used to separate folders. I want to split based on "\\":
String address = "C:\\saeed\\test";
String[] splited = address.split("\\");
However, this is giving me a java.util.regex.PatternSyntaxException.
As others have suggested, you could use:
String[] separated = address.split("\\\\");
or you could use:
String[] separated = address.split(Pattern.quote("\\"));
Also, for reference:
String address = "C:\saeed\test";
will not compile, since \s is not a valid escape sequence. Here \t is interpreted as the tab character, what you actually want is:
String address = "C:\\saeed\\test";
So, now we see that in order to get a \ in a String, we need "\\". The regular expression \\ matches a single backslash since \ is a special character in regex, and hence must be escaped. Once we put this in quotes, aka turn it into a String, we need to escape each of the backslashes, yielding "\\\\".
String#split() method takes a regex. In regex, you need to escape the backslashes. And then for string literals in Java, you need to escape the backslash. In all, you need to use 4 backslashes:
String[] splited = address.split("\\\\");
\ has meaning as a part of the regex, so it too must be quoted. Try \\\\.
The Java will have at \\\\, and produce \\ which is what the regex processor needs to obtain \.
You need to use \\\\ instead of \\.
The backslash(\) is an escape character in Java Strings.If you want to use backslash as a literal you have to type \\\\ ,as \ is also a escape character in regular expressions.
For more details click here
Use separators:
String address = "C:\saeed\test";
String[] splited = address.split(System.getProperty("file.separator"));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
regular expression for DOT
Say I have a String:
String domain = "www.example.com";
To extract the word "example" I am using the split function in java
String[] keys = domain.split(".");
String result = keys[1];
Clearly this is wrong because the "." is a wrong regular expression since it matches any character.
What is the escape sequence which matches specifically the character "."?
Though this question does seem trivial but I can't seem to find any quick reference or previous answers. Thanks.
By escaping it like as follows
\\.
Use \\.. You need to escape it.
You can get the regular expression for any literal string by using Pattern.quote().
Pattern.quote(".") evaluates to "\\."
In this case it would probably be clearer just to use \\.
You can escape . by prefixing it with \\. Hence, use \\. Reason is that the literal string \\ is a single backslash. In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash.
You can escape the . character by using \\. or using the brackets [.].
Hence your code becomes:
String[] keys = domain.split("\\."); // or domain.split("[.]");
String result = keys[1];
Or you could create a class containing the dot, without escaping:
[.]
split this String using function split. Here is my code:
String data= "data^data";
String[] spli = data.split("^");
When I try to do that in spli contain only one string. It seems like java dont see "^" in splitting. Do anyone know how can I split this string by letter "^"?
EDIT
SOLVED :P
This is because String.split takes a regular expression, not a literal string. You have to escape the ^ as it has a different meaning in regex (anchor at the start of a string). So the split would actually be done before the first character, giving you the complete string back unaltered.
You escape a regular expression metacharacter with \, which has to be \\ in Java strings, so
data.split("\\^")
should work.
You need to escape it because it takes reg-ex
\\^
Special characters like ^ need to be escaped with \
This does not work because .split() expects its argument to be a regex. "^" has a special meaing in regex and so does not work as you expect. To get it to work, you need to escape it. Use \\^.
The reason is that split's parameter is a regular expression, so "^" means the beginning of a line. So you need to escape to ASCII-^: use the parameter "\\^".
Well that was pretty much my question, I need to do something like this:
String scriptContent = "print("Hello World")";
Use \".
String scriptContent = "print(\"Hello World\")";
You're looking for something called an escape sequence, which is a way of telling Java to interpret a particular character as something other than what it means by default. In your case, you can make a Java string containing a double-quote by prefixing it with a slash:
String scriptContent = "print(\"Hello World\")";
There are many other escape sequences in Java. For example, \\ stands for a slash character itself (instead of the start of another escape sequence!); \' stands for a single quote; and \n stands for a newline. There are many others; consult a Java reference for more details.
You can use escape character to do this
String scriptContent = "print(\"Hello World\")";
apache commons has a StringEscapeUtils http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html