I'm trying to do regex for splitting string when tab is spotted.
I used this :
String line = scan.nextLine(); String Splitted[] = line.split("\\t");
but it doesn't work so currently I'm using (which is working for me) :
String line = scan.nextLine(); String Splitted[] = line.split("\\\\s\\\\s\\\\s\\\\s");
Do you guys have idea why I can't use the "\\t" regex?
Yes, \\t
is a valid Regex, but in Java string literals, a backslash has a special meaning, so to get the Regex symbol \\t
you'll have to use \\\\t
. But since you are processing user input, you never know what this "tab" really consists of (could be a tab symbol or 4 spaces). So maybe you should just split at (\\\\t|\\\\s{2,})
- beware, this is a Java string literal. Hence the double backslash.
EDIT: In my above answer i suspect you don't want to split at single whitespaces too, is that right? In case you do want to split at single whitespaces , you could really just use \\\\s+
instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.