In java I can split at the last occurrence of a character (dot in this case) like this: String[] parts2 = path.split("\\.(?=[^.]*$)"); String Folderpath = parts2[0]; But when I try to split it at the last occurrence of a slash, like this: String[] parts2 = path.split("\\/(?=[^.]*$)"); String Folderpath = parts2[0]; it gives me following Warning: Redundant character escape '\/' in RegExp
Any help would be appreciated!
You're using the wrong tool for the job.
String folderPath = path.substring(0, path.lastIndexOf('/'));
But your actual problem is that '/' is not special in Java regular expression syntax and therefore does not need to be escaped with '\'. That's what the warning message is telling you. Delete the backslash that precedes '/'.
Related
basically I have:
String str = "Stream: {"stream":null,"_links":{"self":"https://api.twitch.tv/kraken/streams/tfue","channel":"https://api.twitch.tv/kraken/channels/tfue"}}";
I want to split the str by ":{
but when I do:
String[] BuftoStringparts = BuftoString.split("\":{");
I get below exception:
java.util.regex.PatternSyntaxException: Illegal repetition near index 1
":{
^
All replies are much appreciated :)
The main reason this happens:
java.util.regex.PatternSyntaxException: Illegal repetition near index 1 ":{ ^
It's because they are special characters in Java regular expressions so you need to use it escaped for the regex, so by following way:
String[] BuftoStringparts = BuftoString.split("\":\\{");
First of all you need to escape " in your JSON String, so the resulting String will be:
String str = "Stream: {\"stream\":null,\"_links\":{\"self\":\"https://api.twitch.tv/kraken/streams/tfue\",\"channel\":\"https://api.twitch.tv/kraken/channels/tfue\"}}";
Now as mentioned by others, you also need to escape regex special characters in your regex.
You can try your split by following regex:
String[] BuftoStringparts = BuftoString.split("\":\\{");
what is wrong in the following code?
String selectedCountriesStr = countries.replaceAll("[", "").replaceAll("]", "").trim();
String[] selectedCountriesArr = selectedCountriesStr.split(",");
Input String [10000,20000,304050,766666]
Getting error java.util.regex.PatternSyntaxException: Unclosed character class near index 0
You have to escape square brackets because replaceAll() interprets the first argument as a regular expression:
replaceAll("\\[", "")
^^
because, as the error message tells you, the are used for character classes in a regex. Double backslashes are necessary, because "\[" would be an invalid escape sequence. Since the backslash is escaped, the regex engine only receives one backslash.
Also, you can use
replace("[", "")
it will also replace all occurrences of the given CharSequence as is.
You can read more about it in JavaDoc.
Brackets are regex metacharacters, you need to prefix them with a backslash:
.replaceAll("\\[", "").replaceAll("\\]", "")
Also, since this is a simple string substitution, you'd better use .replace():
.replace("[", "").replace("]", "")
String str = "hi,hello,abc,example,problems";
String[] splits = str.split(",");
System.out.println("splits.size: " + splits.length);
for(String asset: splits){
System.out.println(asset);
}
Split function will easily split your string like this
I want to split the following string "Good^Evening" i used split option it is not split the value. please help me.
This is what I've been trying:
String Val = "Good^Evening";
String[] valArray = Val.Split("^");
I'm assuming you did something like:
String[] parts = str.split("^");
That doesn't work because the argument to split is actually a regular expression, where ^ has a special meaning. Try this instead:
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".
The regex you should use is "\^" which you write as "\\^" as a Java String literal; i.e.
String[] parts = "Good^Evening".split("\\^");
The regex needs a '\' escape because the caret character ('^') is a meta-character in the regex language. The 2nd '\' escape is needed because '\' is an escape in a String literal.
try this
String str = "Good^Evening";
String newStr = str.replaceAll("[^]+", "");
I have a String called persons.name
I want to replace the DOT . with /*/ i.e my output will be persons/*/name
I tried this code:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
I am getting StringIndexOutOfBoundsException.
How do I replace the dot?
You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
If you want to replace a simple string and you don't need the abilities of regular expressions, you can just use replace, not replaceAll.
replace replaces each matching substring but does not interpret its argument as a regular expression.
str = xpath.replace(".", "/*/");
Use Apache Commons Lang:
String a= "\\*\\";
str = StringUtils.replace(xpath, ".", a);
or with standalone JDK:
String a = "\\*\\"; // or: String a = "/*/";
String replacement = Matcher.quoteReplacement(a);
String searchString = Pattern.quote(".");
String str = xpath.replaceAll(searchString, replacement);
return sentence.replaceAll("\s",".");
how to split the string in java in Windows?
I used
Eg.
String directory="C:\home\public\folder";
String [] dir=direct.split("\");
I want to know how to split the string in eg.
In java, if I use "split("\")" , there is syntax error.
thanks
split() function in Java accepts regular expressions. So, what you exactly need to do is to escape the backslash character twice:
String[] dir=direct.split("\\\\");
One for Java, and one for regular expressions.
The syntax error is caused because the sing backslash is used as escape character in Java.
In the Regex '\' is also a escape character that why you need escape from it either.
As the final result should look like this "\\\\".
But You should use the java.io.File.separator as the split character in a path.
String[] dirs = dircect.split(Pattern.quote(File.separator));
thx to John
You need to escape the backslash:
direct.split("\\\\");
Once for a java string and once for the regex.
You need to escape it.
String [] dir=direct.split("\\\\");
Edit: or Use Pattern.quote method.
String [] dir=direct.split(Pattern.quote("\\"))
Please, don't split using file separators.
It's highly recommended that you get the file directory and iterate over and over the parents to get the paths. It will work everytime regardless of the operating system you are working with.
Try this:
String yourDir = "C:\\home\\public\\folder";
File f = new File(yourDir);
System.out.println(f.getAbsolutePath());
while ((f = f.getParentFile()) != null) {
System.out.println(f.getAbsolutePath());
}
I guess u can use the StringTokenizer library
String directory="C:\home\public\folder";
String [] dir=direct.split("\");
StringTokenizer token = new StringTokenizer(directory, '\');
while(token.hasTokens()
{
String s = token.next();
}
This may not be completely correct syntactically but Hopefully this will help.
final String dir = System.getProperty("user.dir");
String[] array = dir.split("[\\\\/]",-1) ;
String arrval="";
for (int i=0 ;i<array.length;i++)
{
arrval=arrval+array[i];
}
System.out.println(arrval);
It's because of the backslash. A backslash is used to escape characters. Use
split("\\")
to split by a backslash.
String[] a1 = "abc bcd"
String[] seperate = a1.split(" ");
String finalValue = seperate[0];
System.out.pritln("Final string is :" + finalValue);
This will give the result as abc
split("\\") A backlash is used to escape.