This question already has answers here:
How can I use "." as the delimiter with String.split() in java [duplicate]
(8 answers)
Closed 7 years ago.
I was working on String and tried with split() for . like:-
String s = "Hi.Hello";
System.out.println(Arrays.toString(s.split(".")));
The above code outputs [] empty array. But if I escape . like :-
String s = "Hi.Hello";
System.out.println(Arrays.toString(s.split("\\.")));
Its giving correct output as [Hi, Hello].
So I Want To Know:-
Why do i need to escape . but . it is normally not considered a escape character in String as we can use . directly.
The String.split( String ) method takes a regular expression as argument. In a regular expression "." means "any character", so you have to escape it (by adding "\\"), if you actually want to split by "." and not by "any character".
See the api documentation of Pattern for a starter on the regular expressions.
You cannot split it with just . because split()
Splits this string around matches of the given regular expression.
In regex . means 'match any character', so you have to escape it with \\ instead of \ because you also have to escape the \ character!
Small addition to previous answers (which are correct).
It's often better to use StringUtils.split from Apache Commons Lang (javadoc). It takes plain string (instead of regex) as an argument. Also, its behaviour more intuitive:
StringUtils.split("a..b.c", ".") = ["a", "b", "c"]
vs
"a..b.c".split("\\.") = ["a", "", "b", "c"]
Related
This question already has answers here:
Groovy/Java split string on parentheses "("
(5 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 3 years ago.
I am trying to split a java string with the character "(".
For example :
split("wer(sde")= "wer"+"sde".
But it give exception. Is there a way to split this string using split() function without changing the character "(" to some other character.
String[] cp=cmd.split("{");
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
The thing is, split() receives as parameter a regular expression. Both {} and () are meta-characters and have a special meaning in a regex, so you need to escape them like this:
String[] cp = cmd.split("\\(|\\)");
The method split of String accept a String, that parameter is a regex :
public String[] split(String regex)
Splits this string around matches of the given regular expression.
Since ( is a reserved character in regex, you need to escape it \(.
But in Java, you need to escape twice \\(, once for the String and the second for the regex
This gives :
s.split("\\(");
Parentheses mean something in RegEx, they're used to group characters together. As such, if you intend to reference the literal character, '(' you must escape it within the RegEx:
String[] cp = cmd.split("\\(");
Note the use of two backslashes. This is because the JVM will also interpret a backslash as a metacharacter for escape purposes, so you must escape the backslash itself with another backslash in order for it to make it into the RegEx.
This question already has answers here:
replace String with another in java
(7 answers)
Closed 7 years ago.
I have a string, say 1++++----2 and I want to replace ++++---- with a certain string, say string.
The I use the java function replaceAll, but it keep warning Dangling metacharacter every time I use it:
mystring.replaceAll("++++----", "string");
Escape the +, only the first or all doesn't matter here.
String str = "1+++---2";
str = str.replaceAll("\\+\\+\\+---", "");
System.out.println(str);
Output:
12
Or use replaceAllas it's meant to be used:
str = str.replaceAll("[+-]", "");
replaceAll's first argument takes a regualr expression and + have special meaning in regualr expression. Escape + to make it work properly .
mystring.replaceAll("\\+\\+\\+\\+----", "string");
You can use following regex :
mystring.replaceAll("\\++-+", "string")
Since + is a regex character you need to escape it.so here in "\\++-+" the first part \\+ will match the character + literally and the second + will match 1 or more combination of character + and the rest is -+ which will match 1 or more -.
When you replace a fixed string, you need a replace function:
String mystring = "1++++----2";
System.out.println(mystring.replace("++++----", "string"));
See demo
Otherwise, you need a regex with replaceAll.
System.out.println(mystring.replaceAll("[+]+-+", "string"));
Note that you do not need to escape the + inside a regex character class, which is convenient in Java.
This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 8 years ago.
I've written the following code:
String[] arr = ((String) "asd.asd").split(".");
and arr=[]. Why?
split takes a regular expression as an argument. "." in regular means "any character".
Instead, use:
String[] arr = "asd.asd".split("\\.");
The backslashes escape the special meaning of the "." character in a regular expression.
http://docs.oracle.com/javase/tutorial/essential/regex/
split() accepts a regex. you should escape the . use "\\." . In regex . is a special character (Meta character) which means match any character.
You must double escape the ., otherwise the regular expression represents it as "any character".
Also, you don't need to cast "asd.asd" as String.
String[] arr = "asd.asd".split("\\.");
Because '.' is a special character. You need to escape it by writing it like this '\\.'
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.
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:
[.]