标记化错误:java.util.regex.PatternSyntaxException,悬空元字符 '*'
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,问题是
*
是正则表达式中的保留字符,因此您需要对其进行转义。*
表示“零个或多个先前的表达式”(请参阅Pattern
Javadocs),并且您没有为其提供任何先前的表达式,从而使您的拆分表达式非法。 这就是为什么错误是PatternSyntaxException
。No, the problem is that
*
is a reserved character in regexes, so you need to escape it.*
means "zero or more of the previous expression" (see thePattern
Javadocs), and you weren't giving it any previous expression, making your split expression illegal. This is why the error was aPatternSyntaxException
.我在
regex = "?"
方面也遇到了类似的问题。 对于在正则表达式中具有某种含义的所有特殊字符都会发生这种情况。 因此,您需要将"\\"
作为正则表达式的前缀。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.第一个答案涵盖了它。
我猜测您可能会决定将信息存储在不同的类/结构中。 在这种情况下,您可能不希望 split() 方法的结果进入数组。
你没有要求,但我很无聊,所以这里是一个例子,希望它有帮助。
这可能是您编写的代表单个人的类:
然后,您最初发布的解析代码的版本将如下所示:
(这将它们存储在 LinkedList 中,您可以使用其他东西,例如哈希表等......)
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:
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..)
这是因为 * 被用作元字符来表示前一个字符的一次或多次出现。 所以如果我写 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.:)