5

我正在使用扫描仪和分隔符来标记我的 .txt 文件(这是我必须做的功课)。该文件的第一个版本如下所示:

5,5,5,6,5,8,9,5,6,8, good, very good, excellent, good
7,7,8,7,6,7,8,8,9,7,very good, Good, excellent, very good
8,7,6,7,8,7,5,6,8,7 ,GOOD, VERY GOOD, GOOD, AVERAGE
9,9,9,8,9,7,9,8,9,9 ,Excellent, very good, very good, excellent
7,8,8,7,8,7,8,9,6,8 ,very good, good, excellent, excellent
6,5,6,4,5,6,5,6,6,6 ,good, average, good, good
7,8,7,7,6,8,7,8,6,6 ,good, very good, good,  very good
5,7,6,7,6,7,6,7,7,7  ,excellent, very good, very good, very good

我使用useDelimiter("[ ]*(,)[ ]*")的文件的第二个版本如下所示:

5 5 5 6 5 8 9 5 6 8 good, very good, excellent, good
7 7 8 7 6 7 8 8 9 7 very good, Good, excellent, very good
8 7 6 7 8 7  5 6 8 7 GOOD, VERY GOOD, GOOD, AVERAGE
9 9 9 8 9 7 9  8 9 9 Excellent, very good, very good, excellent
7 8 8 7 8 7 8 9 6 8 very good, good, excellent, excellent
6 5 6 4 5 6 5 6 6 6 good, average, good, good
7  8 7 7 6 8 7 8 6 6 good, very good, good,  very good
5 7 6 7 6 7 6 7 7 7  excellent, very good, very good, very good

而且我想不出一个正则表达式来帮助我用空格分隔数字和用逗号分隔单词。基本上我需要一个有 14 个值的数组(非常好是一个变量)

注意有多个空格(这样做是为了让我们更难)。

因此,我们将不胜感激任何形式的帮助。

PS我们只允许使用分隔符(没有分割等..)

4

4 回答 4

4

这应该有效,关键是正向后视((<?=))和交替(|):

String input = "9 9 9 8 9 7 9  8 9 9 Excellent, very good, very good, excellent";
Scanner s = new Scanner(input).useDelimiter("(?<=\\d)[\\s,]+|\\s*,\\s*");
while (s.hasNext()) {
    System.out.println("Token: ." + s.next() + ".");
}

印刷:

Token: .9.
Token: .9.
Token: .9.
Token: .8.
Token: .9.
Token: .7.
Token: .9.
Token: .8.
Token: .9.
Token: .9.
Token: .Excellent.
Token: .very good.
Token: .very good.
Token: .excellent.
于 2013-02-21T17:22:19.857 回答
2

你可以试试这个(((?<=[0-9]+)\s*(?=[0-9]+))|(,\s*(?=[a-zA-Z]+))|((?<=[0-9]+)\s*(?=[a-zA-Z]+))),看起来很糟糕,但应该可以

于 2013-02-21T16:39:49.397 回答
2

请注意,它Scanner允许您随时更改分隔符。如果您可以依赖输入文本的开头始终包含 10 个数字,结尾始终包含 4 个单词组,那么您可以简单地从仅在空格 ( \s+) 上拆分的分隔符开始,然后在 10 次调用后nextInt()切换到拆分的分隔符在逗号和空格 ( \s*,\s*) 上。

就像是:

String input = "5 5 5 6 5 8 9 5 6 8 good, very good, excellent, good";
Scanner scanner = new Scanner(input).useDelimiter("\\s+");
int[] results = new int[14];
for (int i = 0; i < 10; ++i) {
    results[i] = scanner.nextInt();
}
scanner.useDelimiter("\\s*,\\s*");
scanner.skip("\\s*");
for (int i = 10; i < 14; ++i) {
    String wordPhrase = scanner.next();
    int wordValue;
    if ("average".equalsIgnoreCase(wordPhrase))
        wordValue = 1;
    else if ("good".equalsIgnoreCase(wordPhrase))
        wordValue = 2;
    else if ("very good".equalsIgnoreCase(wordPhrase))
        wordValue = 3;
    else if ("excellent".equalsIgnoreCase(wordPhrase))
        wordValue = 4;
    else
        wordValue = 0;
    results[i] = wordValue;
}

也可以通过使用零宽度环视断言的单个分隔符正则表达式来执行此操作,但这对于简单的作业问题可能有点先进。

于 2013-02-21T16:51:08.547 回答
0
String[] str = expression.split("(,\\s+)|(\\s+)");

试试这个:

于 2013-02-21T16:42:59.007 回答