This question already has answers here:
Why does String.split need pipe delimiter to be escaped?
(3 answers)
Closed 8 years ago.
I am trying to split a string as follows
String string = "mike|ricki"
If I do the following string.split("|") I would expect an array of 2 elements, "mike" and "ricki". Instead I am getting the following
[, m, i, k, e, |, r, i, c, k, i]
Am i doing something fundamentally wrong here?
Yes. Pipe character | is a special character in regular expressions. You must escape it by using \. The escape string would be \|, but in Java the backslash \ is a special character for escape in literal Strings, so you have to double escape it and use \\|:
String[] names = string.split("\\|");
System.out.println(Arrays.toString(names));
If you read the String.split() Java Documentation, it says that it can receive a Regular Expression as an input.
The Pipe character | is a special character in regular expressions so if you want to use it as a literal you have to escape it like \\|
So your code have to be:
String[] splitted = string.split("\\|");
EDIT : Corrected sample code.
String.split takes a regular expression. The pipe character has a special meaning in regex so it's not matching as you were expecting.
Try String.split("\\|") instead.
The backslash tells regex to treat the pipe as a literal character.
Related
This question already has answers here:
java regex pattern unclosed character class
(2 answers)
Closed 6 years ago.
In the GWT tutorial where you build a stock watcher there is this regex expression to check if an input is valid:
if (!symbol.matches("^[0-9A-Z\\.]{1,10}$"))
Which allows inputs between 1 and 10 chars that are numbers, letters, or dots.
The part that confuses me is the \\.
I interpret this as escaped backslash \\ and then a . which stands for any character. And I thought the correct expression would be \. to escape the dot but doing this results in a regex error in eclipse Invalid escape sequence.
Am I missing the obvious here?
This is one of the hassles of regular expressions in Java. That \\ is not an escaped backslash at the regex level, just at the string level.
This string:
"^[0-9A-Z\\.]{1,10}$"
Defines this regular expression:
^[0-9A-Z\.]{1,10}$
...because the escape is consumed by the string literal.
\ is the escape symbol in a Java String Literal. For instance the newline character is written as \n. In order to place a normal \ in a Java string, this is done by using \\.
So your Java String literal (string in the code): "^[0-9A-Z\\.]{1,10}$" is the actual string used for the regular expression "^[0-9A-Z\.]{1,10}$" (with a single slash). So as you expected this is \. in the regular expression.
This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I have a file with content
1|yes|
2|yes|
3|yes|
4|yes|
5|yes|
6|yes|
7|yes|
8|yes|
9|yes|
10|yes|
11|yes|
12|yes|
13|yes|
14|yes|
15|yes|
I use java's String[] tokens = split("|"); to split each line, but it returns (for example splitting "10|yes|") [1,0,|,y,e,s,|]. It seems instead of splitting by "|", it splits every character. Anyone has any idea on it? Thanks!
split accepts a regular expression. | has a specific meaning in regular expressions, it expresses an alternation. To actually split on |, you have to escape it in the regex with a backslash. Since you specify the regex using a string literal, and backslashes are special in string literals, you have to escape that with another backslash:
String[] tokens = str.split("\\|");
In the general case, if you want to use the contents of a string literally, you can use Pattern.quote to automatically escape any special characters. You don't really need it here, but it's useful for end-user-entered values:
String[] tokens = str.split(Pattern.quote(stringToSplitOnLiterally));
This question already has answers here:
Splitting a String with the pipe symbol as delimiter
(6 answers)
Closed 7 years ago.
How can I split my String after this character: |
If i simply write:
String[] parts = match.split("|");
the String is split after every single Character.
Please, escape the character:
String[] parts = match.split("\\|");
Use:
String[] parts = match.split("\\|");
The pipe symbol is a special character for regular expressions; you need to escape it with a backslash if you want to use the literal pipe symbol character. And because the backslash is a special character in Java strings, you need to escape that too with another backslash. Hence, the double backslash before the pipe symbol.
String.split() receives regular expression where | has special meaning. If you want to split by | you have to escape it using back slash \:
String.split("\\|")
The double back slash is needed here to escape the back slash from the point of view of java, and then escape the | from the point of view of regex.
The recommended method is to use:
String[] parts = match.split(Pattern.quote("|"));
this
public void test() {
String match = "A|B";
String[] parts = match.split(Pattern.quote("|"));
System.out.println(Arrays.toString(parts));
}
prints
[A, B]
You need to escape this character. So you can use this :-
String[] parts = match.split("\\|");
So if match is not containing | , the parts will contain only single element , ie match, else the splitted match
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 8 years ago.
I wanna split the content of a string variable, but I wanna use the point as a delimiting regular expression, my code doesn't work.
public class Test {
public static void main(String [] a){
String ch = "r.a.c.h.i.d";
String[] tab;
tab=ch.split(".");
System.out.println(tab.length);
for(String e : tab)System.out.println(e);
}
}
Change tab=ch.split("."); to tab=ch.split("\\.");. You need to escape the dot because otherwise it's treated as a special character in the regex passed to split.
tab = ch.split("\\.");
One slash is the escape character for the regex. But in Java you need to have a second slash because you have to escape the first slash.
Yes, it's possible. In a regular expression, . means any character.
Predefined character classes
. Any character (may or may not match line terminators)
So you must escape it to provide the literal . meaning. Escape it with a backslash character, providing two backslashes, because Java needs the backslash character itself escaped.
Use the regular expression "\\.".
In general, to get the literal characters out of an expression that may contain special-meaning characters, you can use the Pattern.quote method.
This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.
split(Pattern.quote("."))
I have the following code:
String s = "100$ali$Rezaie" ;
String[] ar = s.split("$") ;
The folowing characters do not work in split:
. $ ^
Are there any other characters that will not be accepted in split() method?
The argument to split is a regular expression, not a single character. The page at http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html lists all the characters which have a special meaning in regular expressions.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split%28java.lang.String%29
As the docs say, split takes a regexp as argument. Characters such as ., $ and ^ have special meaning in regexpes.
And it's not to say you couldn't use those characters to split strings. No, you can simply escape the characters in regexp to make them behave "ordinarily".
String[] ar = s.split("\\$");
Use like this
String[] parts = str.split("\\$");
The \ is really equivalent to a single \ (the first \ is required as a Java escape sequence in string literals). It is then a special character in regular expressions which means "use the next character literally, don't interpret its special meaning".
in Java split takes a Regex expression.
Read about regex meta characters here. They are :
the backslash \, the caret ^, the dollar sign $, the period or dot .,
the vertical bar or pipe symbol |, the question mark ?, the asterisk
or star *, the plus sign +, the opening parenthesis (, the closing
parenthesis ), and the opening square bracket [, the opening curly
brace {1
For the most part you can escape these characters with a backslash. In Java you need to escape that backslash with a second backslash. So in order to escape the meta characters you need to use \\.
So in your example:
String[] ar = s.split("\\$") ;
split() method accepts a regular expression as its input. Whatever RegEx has issues with, split() will have issues with that.
Here are the docs for Regex tutorial: http://docs.oracle.com/javase/tutorial/essential/regex/
The Pattern class lists the use of Regular Expression in Java. Any character that you find there has to be escaped if used in regular expression syntax.
If you want to treat that character as regular character you need to escape it.
We can use "\"(double slash) as a prefix and we can split the string..
String s = "100$ali$Rezaie" ;
String[] ar = s.split("\\$") ;
for (String str : ar) {
System.out.println(str);
}