Hi Daniweb.

I'm using a file mask (eg '*.txt') to filter filenames in a program that I'm writing.

It gets the file mask from an .xml settings file.

Currently the mask is '*.xml', and the code I'm using to filter this is like this:

Pattern.matches(fileMask, fileName);

The exception that is thrown (at that line) is the following:

Exception in thread "Thread-0" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*.txt
^

'fileMask', when I print it out, has been assigned th following value: "*.xml" (minus the quotes, ofc)

Does anyone have an idea what I can do to solve this problem? Please let me know if more clarity on the problem is required.

Recommended Answers

All 4 Replies

* is a meta-char in a regex (matches 0 or more repetitions). To use it literally you must precede it with a backslash. To use a backslash as a literal in a Java String literal you need to enter 2 backslashes. So you regex should be "\\*.txt". (Probabaly, I didn't test this)

Thats my problem. I want it to be used as a wildcard, but its not working.

The regex is wildcard is . (dot), (* is the wildcard for Windows filenames etc, but this is a regex). zero or more wildcards would be .* (* means 0 or more repetitions of the preceeding expression). A literal dot must be preceeded by a \, or \\ in a Java String literal.
So maybe ".*\\.txt"

Thanks, I'm going to try that right away

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.