split() function for '$' not working [duplicate] - java

This question already has answers here:
How do I split a string in Java?
(39 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 8 years ago.
I'm doing a simple code
String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("$").length;i++){
System.out.println("I GOT IS :: "+splitString.split("$")[i]);
}
When I split like
splitString.split("$")
It is giving me output [122$23$56$rt]
Why this is not splinting on '$'?

String.split() takes in regex as argument and $ is a metacharacter in Java regex API. Therefore, you need to escape it:
String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("\\$").length;i++){
System.out.println("I GOT IS :: "+splitString.split("\\$")[i]);
}
Other metacharacters supported by the Java regex API are: <([{\^-=!|]})?*+.>

split(Pattern.quote("$"))
Is my favorite.
See Pattern#quote:
Returns a literal pattern String for the specified String.
Your code doesn't work because $ has a special meaning in regex, and since String#split takes a regex as an argument, the $ is not interpreted as the String "$", but as the special meta character $.

Escape it. the split() method takes a regex: split("\\$")

try something like this
String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("\\$").length;i++){
System.out.println("I GOT IS :: "+splitString.split("$")[i]);
}
NOTE: split() uses a regular expression.
Your regular expression uses a special character ie $
$ is the regular expression for "end of line".

String splitString = "122$23$56$rt";
for(int i=0;i<splitString.length;i++){
System.out.println("Now you GOT this :: "+split(Pattern.quote("$")));
}
There are 12 characters with special meanings: 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 {, These special characters are often called "metacharacters".
So your $ is also metacharacter as defination says so you can't split using simple function. Though you must use pattern in this case.
Thanks..

Escape it like
split("\\$")
instead of split("$")

It will not work because split() takes input as RegEx
String splitString = "122$23$56$rt";
for(int i=0;i<splitString.split("\\$").length;i++){
System.out.println("I GOT IS :: "+splitString.split("\\$")[i]);
}

String.split(), .match(), .replaceAll() are some of the methods that use RegEx pattern and so you should look at the javadoc of the Pattern class:
If your splitting character happen to be one of the pattern characters, you must escape it with \\, in this case your split call should be: .split("\\$")

Related

Using Regex for splitting a string in java [duplicate]

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?

How to split a java string with "("? [duplicate]

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.

Java String Split Behavior - Split on Dollar Symbol [duplicate]

This question already has answers here:
Why can't I split a string with the dollar sign?
(6 answers)
Closed 7 years ago.
I am trying the following code (running java version 1.7 in Eclipse Luna IDE on Ubuntu Linux 12.04):
String str = "abc$xyz";
String[] split_ = str.split("$");
System.out.println(split_.length);
I am always getting a split of length 1. If I try to print split_[0], I am always getting the entire string. Can anyone suggest what might be the cause?
This is because split expects a regular expression. Since "$" is the end-of-line marker in a regular expression, this only splits on the end of the String.
You should use
String str = "abc$xyz";
String[] split_ = str.split("\\$");
System.out.println(split_.length);
instead.
This escapes the "$", so that it's treated as a literal character instead (and uses two slashes to escape the backslash as part of the string literal).
The $ character is a metacharacter meaning "end of line", not a literal dollar sign.
Escape the $ character with two backslashes, one to escape the $ in the regular expression, one for a Java escape for a backslash.
String[] split_ = str.split("\\$");
.split() uses regex that is why...
Try this:
String str = "abc$xyz";
String[] split_ = str.split("\\$");
System.out.println(split_.length);

Why does split(".") fail? java [duplicate]

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 '\\.'

java regex illegal escape character error not occurring from command line arguments [duplicate]

This question already has answers here:
Why does this Java regex cause "illegal escape character" errors?
(7 answers)
Closed 3 years ago.
This simple regex program
import java.util.regex.*;
class Regex {
public static void main(String [] args) {
System.out.println(args[0]); // #1
Pattern p = Pattern.compile(args[0]); // #2
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.println(m.start()+" "+m.group());
}
}
}
invoked by java regex "\d" "sfdd1" compiles and runs fine.
But if #1 is replaced by Pattern p = Pattern.compile("\d");, it gives compiler error saying illegal escape character. In #1 I also tried printing the pattern specified in the command line arguments. It prints \d, which means it is just getting replaced by \d in #2.
So then why won't it throw any exception? At the end it's string argument that Pattern.compile() is taking, doesn't it detect illegal escape character then? Can someone please explain why is this behaviour?
A backslash character in a string literal needs to be escaped (preceded by a backslash). When passed in from the command line the string is not a string literal. The compiler complains because "\d" is not a valid escape sequence (see Escape Sequences for Character and String Literals ).
The \ character is used as an escape character for both Java string literals and regular expressions. This confuses many programmers. When you want to create a String in Java to represent a regular expression that has an escape character then you need to escape the Java escape character.
When passing the string in on the command line the JVM handles this for you and simply creates the String.
What you want is this
Pattern p = Pattern.compile("\\d");
The backslash \ in Java results in an escape in strings. For example, the string "\t" results in a tab character in java. This is also why "\n" produces a newline.
In regular expressions, \d is an escape with respect to the regular expression, not Java. This means in order to get \d in a string literal, you have to type "\\d" in the string. Basically, you have to escape the \ to get the literal value \d, and then when Pattern compiles the regex, it further escapes the \d to be parsed as a digit.
This can be confusing, but long story short, you should never have a single \ in a string literal for a regular expression since even the string literal "\\n" gets parsed properly.
I'm not entirely sure if I understand the question, but it seems like your problem is that you're treating "\d" as a Java escape character, which doesn't exist. To treat it as a regex escape character, use "\d" to escape the Java escape.

Categories

Resources