0

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.

Edited by P00dle: n/a

2
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by P00dle
0

* 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)

0

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

0

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"

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.