This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell 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


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: 18874
    
  40

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: 24187
    
  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..
 
GeeCON Prague 2014
 
subject: Dangling meta character '*' near index 0 (java.util.regex.PatternSyntaxException)