标记化错误:java.util.regex.PatternSyntaxException,悬空元字符 '*'

发布于 2024-07-21 04:20:18 字数 1076 浏览 6 评论 0原文

我正在使用 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();
    }

并且我收到这个异常:

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

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

I am using split() to tokenize a String separated with * following this format:

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

I'm reading this from a file named "entrada.al" using this code:

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();
    }

And I'm getting this exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*

My guess is that the lack of a * after age on the original text file is causing this. How do I get around it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多孤肩上扛 2024-07-28 04:20:18

不,问题是 * 是正则表达式中的保留字符,因此您需要对其进行转义。

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

* 表示“零个或多个先前的表达式”(请参阅​​ Pattern Javadocs),并且您没有为其提供任何先前的表达式,从而使您的拆分表达式非法。 这就是为什么错误是 PatternSyntaxException

No, the problem is that * is a reserved character in regexes, so you need to escape it.

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

* means "zero or more of the previous expression" (see the Pattern Javadocs), and you weren't giving it any previous expression, making your split expression illegal. This is why the error was a PatternSyntaxException.

挽袖吟 2024-07-28 04:20:18

我在 regex = "?" 方面也遇到了类似的问题。 对于在正则表达式中具有某种含义的所有特殊字符都会发生这种情况。 因此,您需要将 "\\" 作为正则表达式的前缀。

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

I had similar problem with regex = "?". It happens for all special characters that have some meaning in a regex. So you need to have "\\" as a prefix to your regex.

String [] separado = line.split("\\*");
故事还在继续 2024-07-28 04:20:18

第一个答案涵盖了它。

我猜测您可能会决定将信息存储在不同的类/结构中。 在这种情况下,您可能不希望 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 中,您可以使用其他东西,例如哈希表等......)


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();
}

The first answer covers it.

Im guessing that somewhere down the line you may decide to store your info in a different class/structure. In that case you probably wouldn't want the results going in to an array from the split() method.

You didn't ask for it, but I'm bored, so here is an example, hope it's helpful.

This might be the class you write to represent a single person:


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.
} 

Then, the version of the parsing code you originally posted would look something like this:
(This stores them in a LinkedList, you could use something else like a Hashtable, etc..)


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();
}

嗫嚅 2024-07-28 04:20:18

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

It is because * is used as a metacharacter to signify one or more occurences of previous character. So if i write M* then it will look for files MMMMMM..... ! Here you are using * as the only character so the compiler is looking for the character to find multiple occurences of,so it throws the exception.:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文