Okay, so I'm having problems with my code. I've browsed Daniweb and couldn't find any solutions on here that work.

I'm extracting 2 sets of texts from a .xml file. The first part is a directory name(eg. "C:\work\") and the second part is the name of a file in that directory.

I concatenate the 2 strings and then test it against a filename checker.

The exception I get is this:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 3
C:\Thread test\P1\kk.txt

The relevant code is here(I've highlighted the line that throws the exception):

public static void main(String[] args) {

        while (true) {
            // Call scan() method
            new ThreadStarter().scan();


    void scan() {
        System.out.println("starting scanning");

        // Go through the list of priority Dir's first, then the normal dirs.
        for (int i = 0; i < priorityDirs.length; i++) {
            String used = location + priorityDirs[i];
            filePickup(used, true);

    public static class FilenameFilterRegex implements FilenameFilter {

        String strRegex = "";

        public boolean accept(File dir, String name) {
            //return Pattern.matches(".*\\.(jpg|jpeg|gif|png|bmp)", name);
            //if only one extension to check :  "\\.jpg"
            [B]return Pattern.matches(strRegex, name);[/B]

        public void aregex(String straRegex) {
            strRegex = straRegex;
            //return (strRegex);

    void filePickup(String dir, boolean bool) {
        try {
            // Scan for files
            File main_dir = new File(dir);

            File[] children = main_dir.listFiles();

            // Checks if files were found
            if (children.length > 0) {
                for (int ii = 0; ii < children.length; ii++) {
                    // Make sure only files are scanned in, not directories, etc.
                    if (children[ii].isFile()) {
                        // Get fileName, call Threader in Threader, send fileName
                        String fileName = children[ii].getAbsolutePath();
                        FilenameFilterRegex FF = new FilenameFilterRegex();
                        File file = new File("");

                        if (FF.accept(file, fileName)) {
                                    // Start a thread count
                                    new Thread(new Threader(this, fileName, 1)).start();
        } catch (InterruptedException ie) {

Can anyone please tell me what the problem is?

I don't know whats causing the error, and pasting the exception itself in Google just gives me vague answers.

If someone could clarify the problem for me I'd be extremely grateful.

Recommended Answers

All 2 Replies

replace all single slants with double slants before (or while) calling "aregex".

str.replace("\\", "\\\\")

note: replace, not replaceFirst or replaceAll.

Lol... That solved it.

I was using

str.replaceAll("\\", "\\\\");

earlier and it didn't work.

Never thought to just use .replace

Thank you.

Be a part of the DaniWeb community

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