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?