0

我需要使用 scan.useDelimiter() 来识别在“:”之后发生的字符串。

我正在制作一个扫描仪,它从类似条目列表中获取以下行。

c : 20002 : The Dragon :     Dreama : 10000 :   1 :  22 :   9 :  237.20 :   60.47 :  354.56

这是我到目前为止所拥有的:

    public Boolean readFile (){ //Fix tomorrow
    while ( input.hasNext () ) {
        char x =                stat.next().charAt(0);
        String line =           input.nextLine().trim();
        Scanner stat =          new Scanner(line).useDelimiter("\\s*:\\s*")

        if( line.length()== 0 || input.next().charAt(0) == '/' ) continue;
        switch ( x ) {
            case 'c' :
                int a =         stat.nextInt();
                String b =      stat.next();
                break;
            case 't' :
                //...

由于某种原因,我无法正确扫描它。对于上面的条目,我需要以下字符串。

20002, The Dragon, Dreama, 10000, etc.

如您所见,我需要忽略短语边缘的空格,但如果在诸如“The Dragon”之类的短语之间有空格,则需要保留。

以下问题对我没有帮助: 问题 1 / 问题 2

4

5 回答 5

4

只需使用String.split()

String line = input.nextLine().trim();
String[] tokens = line.split("\\s*:\\s*");

请注意,我只是在您的问题中重用正则表达式。您的正则表达式将删除每个标记中的所有冗余前导和尾随空格。整个trim()输入仍然需要删除前导和尾随空格,split()方法中的正则表达式不匹配并且不会触及。

于 2013-06-12T16:00:49.703 回答
3

您可能要考虑改用String.split

所以你最终会拥有 -

while ( input.hasNext () ) {
    char x =                input.next().charAt(0);
    String[] words =           input.nextLine().split(":");
    for(String s : words){
        s.trim();
    }
    ....

另外,我不认为你需要得到你的第一个字符。我没有看到你的代码,所以我不知道。也许你稍后会使用它,但它对我来说似乎无关紧要。

于 2013-06-12T16:00:03.157 回答
2

描述

要使用正则表达式匹配:分隔字符串,您可以使用此表达式,它还过滤分隔符周围的空格

(?::)\s*([^:]*)\s*(?=:|$)

在此处输入图像描述

Java 代码示例

在此示例中,您将对从捕获组 1 收集的值感兴趣

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "c : 20002 : The Dragon :     Dreama : 10000 :   1 :  22 :   9 :  237.20 :   60.47 :  354.56";
  Pattern re = Pattern.compile("(?::)\\s*([^:]*)\\s*(?=:|$)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
  Matcher m = re.matcher(sourcestring);
  int mIdx = 0;
    while (m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
        System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
      }
      mIdx++;
    }
  }
}

产量

$matches Array:
(
    [0] => Array
        (
            [0] => : 20002 
            [1] => : The Dragon 
            [2] => :     Dreama 
            [3] => : 10000 
            [4] => :   1 
            [5] => :  22 
            [6] => :   9 
            [7] => :  237.20 
            [8] => :   60.47 
            [9] => :  354.56
        )

    [1] => Array
        (
            [0] => 20002 
            [1] => The Dragon 
            [2] => Dreama 
            [3] => 10000 
            [4] => 1 
            [5] => 22 
            [6] => 9 
            [7] => 237.20 
            [8] => 60.47 
            [9] => 354.56
        )

)
于 2013-06-12T18:52:57.173 回答
2

您可以使用split(),但.useDelimiter()执行相同的工作(仅使用不同的输出)。

您需要的是首先获取 char,然后继续进行其余操作。也许这会给你一个线索:

public Boolean readFile (){ //Fix tomorrow
    while( input.hasNext () ) {
       char x =           input.next().charAt(0); // get the char
       System.out.println("char obtained: "+x);

       String line =      input.nextLine().trim();
       Scanner stat =     new Scanner(line).useDelimiter("\\s*:\\s*")

       while ( stat.hasNext() ) {
          if (stat.hasNext("")||stat.hasNext(Pattern.compile("/.*"))) continue;
          // above line is equivalent to:
          // if( token.length()== 0 || token.charAt(0) == '/' ) continue;
          // but is better because does not consume a token
          switch ( x ) {
              case 'c' :
                  int a =         stat.nextInt(); // 20002
                  String b =      stat.next(); // "The Dragon"
                  System.out.println("b obtained: "+b);
                  // some options here:
                  // - add a line for each expected var like:
                  String b2 =      stat.next(); // "Dreama"
                  String i =      stat.nextInt(); // 10000
                  String j =      stat.nextInt(); // 1
                  // ...

                  // OR: - iterate through the remaining tokens and do something
                  // while (stat.hasNext()) { System.out.println(stat.next()); }

                  // OR: - get an array of the remaining tokens using split():
                  // String[] restOfLine = stat.nextLine().split("\\s*:\\s*");
                  break;
              case 't' :
                  //...
于 2013-06-12T16:22:09.860 回答
2

您没有按照应有的方式使用 Scanner:您必须遍历找到的令牌:

String line = "c : 20002 : The Dragon :     Dreama : 10000 :   1 :  22 :   9 :  237.20 :   60.47 :  354.56";
Scanner scanner = new Scanner(line).useDelimiter("\\s*:\\s*");
while (scanner.hasNext()) {
    String token = scanner.next();
    // do you stuff with the token 
    System.out.println(token);
}

这打印:

c
20002
The Dragon
Dreama
10000
1
22
9
237.20
60.47
354.56

这似乎是你需要的。

于 2013-06-12T16:10:13.640 回答