Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tokenizing Error: java.util.regex.PatternSyntaxException, dangling metacharacter '*'

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?

like image 823
andandandand Avatar asked May 27 '09 20:05

andandandand


1 Answers

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.

like image 96
Michael Myers Avatar answered Sep 19 '22 06:09

Michael Myers