File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Dangling meta character '*' near index 0 (java.util.regex.PatternSyntaxException) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dangling meta character Watch "Dangling meta character New topic

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

Rakesh Rajmohan

Joined: Jul 09, 2009
Posts: 22

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

Joined: Sep 28, 2004
Posts: 20531

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

"*" 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.

[Jess in Action][AskingGoodQuestions]
Rakesh Rajmohan

Joined: Jul 09, 2009
Posts: 22
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..
I agree. Here's the link:
subject: Dangling meta character '*' near index 0 (java.util.regex.PatternSyntaxException)
It's not a secret anymore!