regex不知道该怎么做。split()在Java中工作
我从一个文本文件中获取了这个字符串
"1 normal 1 [(o, 21) (o, 17) (t, 3)]"
我想在字符串数组中接受1, normal, 1, o, 21, 17, t, 3
Scanner inFile = new Scanner(new File("input.txt");
String input = inFile.nextLine();
String[] tokens = input.split(" |\\(|\\)|\\[\\(|\\, |\\]| \\(");
for(int i =0 ; i<tokens.length; ++i)
{
System.out.println(tokens[i]);
}
输出:
1
normal
1
o
21
o
17
t
3
为什么阵列中存储空间
# 1 楼答案
这不是空格,这是空字符串。您的字符串是:
它根据您的regexp按以下方式拆分:
当两个相邻的分隔符出现时,认为它们之间有一个空字符串标记
要解决此问题,您可以更改regexp,例如:
因此,任何数量的
" ()[,]"
相邻字符都将被视为分隔符# 2 楼答案
例如:
[(
因此,在这两个匹配之间,将返回一个空字符串“”