Hi Daniweb.

I just read the other threads on regex and couldn't find the help I am looking for.

The problem is simple.

I have a bunch of settings in a .xml file which I read into my program.

One of the settings is '*.txt', which is used for regex as a file mask.

I need to check all the files that the program handles and make sure their file name matches '*.txt' (the * meaning there can be anything in front of .txt, of course).

The error I used to get was that there was a dangling meta-character. I solved that problem by replacing the * with '.*' and the .txt with '\\.txt' after the setting is read in.

The program no longer throws an error, but it doesn't recognize the right files, either. it just ignores all files, whether they match or not.

Here is the part where I edit the '*.txt' and do the regex:

public static class FilenameFilterRegex implements FilenameFilter {

        private String strRegex = "";

        public String aregex(String straRegex) {
            straRegex = straRegex.replace(".", "\\\\.");
            straRegex = straRegex.replace("*", ".*");
            strRegex = straRegex;
            return (strRegex);
        }

        public boolean accept(File dir, String name) {
            return Pattern.matches(strRegex, name);
        }
    }

Any idea whats going wrong?

Recommended Answers

All 7 Replies

How about some basic debugging? after line 12 print the name and the strRegex to see if they are both what you expected.
ps does this mean that yesterdays dangling metachar thread is now "solved"?

Just a guess, but do you actually call aregex() in there somewhere? If not, your strRegex is going to be "" when you're calling accept().

Also, you don't use the parameter "dir" in accept() - could this be part of your problem?

Hi jon. The dir param isn't relevant to the problem as stated - he just wants to filter by file name regardless of directory.
the aragex(..) method certainly looks odd; is it intended to return a result or set a local variable, or both (in which case why?). And we have no evidence that this particular FilenameFilter is even being used at all...
hence my "try debugging it" suggestion.
J

The dir was an afterthought - anytime I see a method that requires but doesn't use a parameter, it sets off a flag for me.

I agree - aregex() looks like a likely culprit. The assignment of local to global and followed by a return of same is very cute. This is, I think, the "Carbon Copy" design pattern, used when a class needs to keep duplicate copies of all its paperwork.

I've debugged, yes.

The file mask prints out as '.*\\.txt' and the filename(November.txt - just a test file) is what it is supposed to be, thanks.

I honestly don't know why aregex returns anything. The value it returns is never used anywhere. However, my team leader insisted on having it that way, so nothing I can do about that.

Sorry for only replying now. Thanks for the quick responses and the positive attitudes!

Your mask should print as ".*\.txt" -- you should only see the double backslash when entering a Java String literal. You have doubled the backslashes once too many in line 6, which should be replace(".", "\\.");

Thx guys, It's sorted out now.

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.