58

我用来标记一个用以下格式split()分隔的字符串:*

name*lastName*ID*school*age
%
name*lastName*ID*school*age
%
name*lastName*ID*school*age

我正在使用以下代码从名为“entrada.al”的文件中读取此内容:

static void leer() {

    try {
        String ruta="entrada.al";
        File myFile = new File (ruta);
        FileReader fileReader = new FileReader(myFile);

        BufferedReader reader = new BufferedReader(fileReader);

        String line = null;

        while ((line=reader.readLine())!=null){
            if (!(line.equals("%"))){
                String [] separado = line.split("*"); //SPLIT CALL
                names.add(separado[0]);
                lastNames.add(separado[1]);
                ids.add(separado[2]);
                ages.add(separado[3]);
            }
        }

        reader.close();
    }

我得到了这个例外:

线程“主”java.util.regex.PatternSyntaxException 中的异常:在索引 0 附近悬空元字符 '*' *

我的猜测是*原始文本文件缺少后年龄导致了这种情况。我该如何解决?

4

4 回答 4

154

不,问题是它*是正则表达式中的保留字符,所以你需要转义它。

String [] separado = line.split("\\*");

*表示“前一个表达式的零个或多个”(参见PatternJavadocs),并且您没有给它任何前一个表达式,从而使您的拆分表达式非法。这就是为什么错误是PatternSyntaxException.

于 2009-05-27T20:22:28.180 回答
7

我有类似的问题regex = "?"。它发生在所有在正则表达式中具有某种意义的特殊字符上。因此,您需要将其"\\"作为正则表达式的前缀。

String [] separado = line.split("\\*");
于 2014-05-26T06:50:35.553 回答
3

第一个答案涵盖了它。

我猜想你可能会决定将你的信息存储在不同的类/结构中。在这种情况下,您可能不希望结果从 split() 方法进入数组。

你没有要求它,但我很无聊,所以这里是一个例子,希望它有帮助。

这可能是您编写的代表一个人的类:


class Person {
            public String firstName;
            public String lastName;
            public int id;
            public int age;

      public Person(String firstName, String lastName, int id, int age) {
         this.firstName = firstName;
         this.lastName = lastName;
         this.id = id;
         this.age = age;
      }  
      // Add 'get' and 'set' method if you want to make the attributes private rather than public.
} 

然后,您最初发布的解析代码版本将如下所示:(这将它们存储在 LinkedList 中,您可以使用其他东西,如 Hashtable 等。)


try 
{
    String ruta="entrada.al";
    BufferedReader reader = new BufferedReader(new FileReader(ruta));

    LinkedList<Person> list = new LinkedList<Person>();

    String line = null;         
    while ((line=reader.readLine())!=null)
    {
        if (!(line.equals("%")))
        {
            StringTokenizer st = new StringTokenizer(line, "*");
            if (st.countTokens() == 4)          
                list.add(new Person(st.nextToken(), st.nextToken(), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken)));         
            else            
                // whatever you want to do to account for an invalid entry
                  // in your file. (not 4 '*' delimiters on a line). Or you
                  // could write the 'if' clause differently to account for it          
        }
    }
    reader.close();
}

于 2009-05-28T02:02:13.177 回答
3

这是因为 * 用作元字符来表示前一个字符的一个或多个出现。所以如果我写 M* 那么它会寻找文件 MMMMMM .....!在这里,您使用 * 作为唯一字符,因此编译器正在寻找该字符以查找多次出现,因此它会引发异常。:)

于 2011-09-05T09:09:49.440 回答