| 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: 16695
|
|
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: 24057
|
|
"*" 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..
|
 |
 |
|
|
subject: Dangling meta character '*' near index 0 (java.util.regex.PatternSyntaxException)
|
|
|