• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dangling meta character '*' near index 0 (java.util.regex.PatternSyntaxException)

 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello..

Am using a pattern - Pattern p1 = Pattern.compile("*(.data)"); but I get the above error..

What I want to find is all '*.data' iles from a directory..

Thanks for your help!!
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to consider picking up a good book on regex. Its a good thing to learn, as regex is an important tool.

But back to your question... A "*" means zero or more of the previous. So, a "*" as the first character doesn't make sense.

Henry
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"*" is a special character in regular expressions (as is "."); it means "zero or more matches of the preceding pattern", which is not the same as what it means when matching filenames at the command line (using those patterns is often called "globbing"). A regular expression that matches files named "*.data" would be



This means "one or more characters followed by ".data". The first dot is used in its regular expression meaning of "any character", but the second dot should match only a dot, so we've escaped it with a backslash.
 
Rakesh Rajmohan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool.. Thanks for your responses.. I tried using "[a-zA-Z]*\\.data" and it worked.. But if it had any special chars, it wasn't working.. So I tried out your ".+\\.data" and it worked like a GEM..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic