This question already has answers here:
String.split returning null when using a dot
(4 answers)
Closed 9 years ago.
I have to take an input file, and append a number at the end to its name to use as output file. To achieve this, I use the following code:
String delimiter = ".";
String[] splitInput = inputLocation.split(delimiter);
String outputLocation = splitInput[0];
and I get the following exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
I added the following statement to check the length of the splitInput array, and I get 0 as output.
System.out.println(splitInput.length);
Later, I used ".x" as delimiter (my file being .xls). I can use ".x" and achieve my purpose but I'm curious why won't "." work?
The split function uses regular expressions, you have to escape your "." with a "\"
When using regular expressions a "." means any character. Try this
String delimiter = "\\.x";
It should also be mentioned that \ in java is also a special character used to create other special characters. Therefore you have to escape your \ with another \ hence the "\\.x"
Theres some great documentation in the Java docs about all the special characters and what they do:
Java 8 Docs
Java 7 Docs
Java 6 Docs
The . has a special meaning: Any character (may or may not match line terminators). You can escape it prepending \ or use:
[.]x
e.g.:
String delimiter = "[.]x";
See more in http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
String.split() expects a regex as input. In Java regexes, . character is a special character. Thus, your split statement is not working the way you expected. You should escape your "." as \\..
. is considered as any character in regex. Please use escape character \ (which also needs to be escaped as \\), if you want to override the special meaning of it.
Related
This question already has answers here:
Java regular expressions and dollar sign
(5 answers)
Closed 3 years ago.
How to replace all "$$$" present in a String?
I tried
story.replaceAll("$$$","\n")
This displays a warning: Anchor $ in unexpected position and the code fails to work. The code takes the "$" symbol as an anchor for a regular expression. I just need to replace that symbol.
Is there any way to do this?
"$" is a special character for regular expressions.
Try the following:
System.out.println(story.replaceAll("\\$\\$\\$", "\n"));
We are escaping the "$" character with a '\' in the above code.
There are several ways you can do this. It depends on what you want to do, and how elegant your solution is:
String replacement = "\n"; // The replacement string
// The first way:
story.replaceAll("[$]{3}", replacement);
// Second way:
story.replaceAll("\\${3}", replacement);
// Third way:
story.replaceAll("\\$\\$\\$", replacement);
You can replace any special characters (Regular Expression-wise) by escaping that character with a backslash. Since Java-literals use the backslash as escaping-character too, you need to escape the backslash itself.
story.replaceAll("\\${3}", something);
By using {3}behind the $, you say, that it should be found exactly three times. Looks a bit more elegant than "\\$\\$\\$".
something is thus your replacement, for example "" or \n, depending on what you want.
this will surely work..
story.replaceAll("\\$\\$\\$","\n")
YOu can do this for any special character.
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:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Closed 7 years ago.
I tried to read about regex and escaping, but no luck.
I have a string that looks like this:
String s = "4/18/2015|Planned|Linux|Maintenance";
And I want to split it with the delimiter '|' :
String[] tokens = s.split("|");
The correct results I am expecting which are
tokens[0] is "4/18/2015",
tokens[1] is "Planned",
tokens[2] is "Linux",
token[3] is "Maintenance",
yet it's giving me some weird result like this:
tokens[0] is null
tokens[1] is 4
tokens[2] is /
and tokens[3] is 1
I am guessing it's because of the slashes '/' in the date that's why. I tried to search for many existing questions and tried the suggested methods as well but to no avail.
#mushfek0001 got it right.
The pipe in most regex dialects is a metacharacter for the alternation; basically what you ask the regex engine to do here is: "split against the empty string or... the empty string".
And, uh, it means you would potentially get empty each time, except that the regex engine is not a fool, and if an empty match is detected in a split the engine will advance one character before splitting again... Hence your result (not sure why the first element is null and not the empty string, though).
Therefore, you should split against "\\|", not "|".
What is more, if you do this repeatedly, use a Pattern instead:
private static final Pattern PIPE = Pattern.compile("\\|");
// ...
final String[] tokens = PIPE.split(yourInput);
Just use
split("\\x7C")
or
split("\\|")
You need to escape or use corresponding unicode value when splitting against the pipeline char '|'.
escape the pipe character:
s.split("\\|");
because pipe sign in regex means OR, so to escape it you need \| but in regex you need to escape \ too so \\| will work.
or as mushfek0001 suggested:
split("\\x7C")
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The split() method in Java does not work on a dot (.)
I'm new to java. I want to split a String from "." (dot) and get those names one by one. But this program gives error: "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0"
please help me
String input1 = "van.bus.car";
System.out.println(input.split(".")[0]+"");
System.out.println(input.split(".")[1]+"");
System.out.println(input.split(".")[2]+"");
In regex, Dot(.) is a special meta-character which matches everything.
Since String.split works on Regex, so you need to escape it with backslash if you want to match a dot.
System.out.println(input.split("\\.")[0]+"");
To learn more about Regex, refer to following sites: -
http://docs.oracle.com/javase/tutorial/essential/regex/
http://www.vogella.com/articles/JavaRegularExpressions/article.html
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
The argument to split is a regex, and so the full stop/dot/. has a special meaning: match any character. To use it literally in your split, you'll need to escape it:
String[] splits = input1.split("\\.");
That should give you an array of length 3 for your input string.
For more about regex and which characters are special, see the docs for Pattern.
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.