This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Dangling meta character Watch "Dangling meta character New topic
Author

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

Rakesh Rajmohan
Greenhorn

Joined: Jul 09, 2009
Posts: 22
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
Sheriff

Joined: Sep 28, 2004
Posts: 19926
    
  43

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


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

Joined: Jul 08, 2003
Posts: 24189
    
  34

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

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..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dangling meta character '*' near index 0 (java.util.regex.PatternSyntaxException)
 
It's not a secret anymore!