count files with regex in java












1















I want to count files using regex to match filenames.But My regex didn't match. My "F:" has two files named "a(1).txt"&"a(1)(1).txt" .But regex only can match one of them,the count is 1. But when I change "()" to "-" in my regex and files' names.It can match all of them.I think the problem is in my regex.But I don't know why?
here is my code:



public static void main(String args) {
File dest = new File("F:\");
File file = new File("E:\a(1).txt");
move(file, dest);
}

public static void move(File file, File dest) {
//get file name
String name = file.getName();
int index = name.lastIndexOf(".");
String realname = name.substring(0, index);
String suffix = name.substring(index + 1, name.length());
//get files in F:
File fs = dest.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//get File name
String fname = pathname.getName();
return fname.equals(name) || fname.matches(realname + "\(\d+\)." + suffix);
}
});
int count = fs.length;
System.out.println(count);
file.renameTo(new File(dest + realname + (count == 0 ? "." : "(" + count + ").") + suffix);
}









share|improve this question

























  • Typo: liatFiles

    – Pavitra
    Nov 25 '18 at 3:55
















1















I want to count files using regex to match filenames.But My regex didn't match. My "F:" has two files named "a(1).txt"&"a(1)(1).txt" .But regex only can match one of them,the count is 1. But when I change "()" to "-" in my regex and files' names.It can match all of them.I think the problem is in my regex.But I don't know why?
here is my code:



public static void main(String args) {
File dest = new File("F:\");
File file = new File("E:\a(1).txt");
move(file, dest);
}

public static void move(File file, File dest) {
//get file name
String name = file.getName();
int index = name.lastIndexOf(".");
String realname = name.substring(0, index);
String suffix = name.substring(index + 1, name.length());
//get files in F:
File fs = dest.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//get File name
String fname = pathname.getName();
return fname.equals(name) || fname.matches(realname + "\(\d+\)." + suffix);
}
});
int count = fs.length;
System.out.println(count);
file.renameTo(new File(dest + realname + (count == 0 ? "." : "(" + count + ").") + suffix);
}









share|improve this question

























  • Typo: liatFiles

    – Pavitra
    Nov 25 '18 at 3:55














1












1








1








I want to count files using regex to match filenames.But My regex didn't match. My "F:" has two files named "a(1).txt"&"a(1)(1).txt" .But regex only can match one of them,the count is 1. But when I change "()" to "-" in my regex and files' names.It can match all of them.I think the problem is in my regex.But I don't know why?
here is my code:



public static void main(String args) {
File dest = new File("F:\");
File file = new File("E:\a(1).txt");
move(file, dest);
}

public static void move(File file, File dest) {
//get file name
String name = file.getName();
int index = name.lastIndexOf(".");
String realname = name.substring(0, index);
String suffix = name.substring(index + 1, name.length());
//get files in F:
File fs = dest.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//get File name
String fname = pathname.getName();
return fname.equals(name) || fname.matches(realname + "\(\d+\)." + suffix);
}
});
int count = fs.length;
System.out.println(count);
file.renameTo(new File(dest + realname + (count == 0 ? "." : "(" + count + ").") + suffix);
}









share|improve this question
















I want to count files using regex to match filenames.But My regex didn't match. My "F:" has two files named "a(1).txt"&"a(1)(1).txt" .But regex only can match one of them,the count is 1. But when I change "()" to "-" in my regex and files' names.It can match all of them.I think the problem is in my regex.But I don't know why?
here is my code:



public static void main(String args) {
File dest = new File("F:\");
File file = new File("E:\a(1).txt");
move(file, dest);
}

public static void move(File file, File dest) {
//get file name
String name = file.getName();
int index = name.lastIndexOf(".");
String realname = name.substring(0, index);
String suffix = name.substring(index + 1, name.length());
//get files in F:
File fs = dest.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
//get File name
String fname = pathname.getName();
return fname.equals(name) || fname.matches(realname + "\(\d+\)." + suffix);
}
});
int count = fs.length;
System.out.println(count);
file.renameTo(new File(dest + realname + (count == 0 ? "." : "(" + count + ").") + suffix);
}






java regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 4:53









uli

519313




519313










asked Nov 25 '18 at 3:41









HanHan

84




84













  • Typo: liatFiles

    – Pavitra
    Nov 25 '18 at 3:55



















  • Typo: liatFiles

    – Pavitra
    Nov 25 '18 at 3:55

















Typo: liatFiles

– Pavitra
Nov 25 '18 at 3:55





Typo: liatFiles

– Pavitra
Nov 25 '18 at 3:55












2 Answers
2






active

oldest

votes


















0














You should learn debugging with IDE or at least printing values at sum checkpoint to narrow the source of issue.



Your pattern for fname.matches() is a(1)(d+).txt. Round brackets are special symbols in regex to escape them you could use java.util.regex.Pattern.quote(realname). It will change the pattern to a(1)(d+).txt.



Thoughts:

input file = a(2).txt to match a(2).txt and a(2)(1).txt you could remove fname.equals(name) and modify regex to fname.matches(Pattern.quote(realname) + "(\(\d+\))*\." + suffix). * means zero or more times inside round brackets; "." - dot is a special symbol and should be escaped.






share|improve this answer































    0














    I think your regex should be



    ((d+))+.


    You also need to escape special character to use this regex in Java code as followed:



    (\(\d+\))+\.





    share|improve this answer
























    • The issue is with realname.

      – uli
      Nov 25 '18 at 4:56











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464460%2fcount-files-with-regex-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should learn debugging with IDE or at least printing values at sum checkpoint to narrow the source of issue.



    Your pattern for fname.matches() is a(1)(d+).txt. Round brackets are special symbols in regex to escape them you could use java.util.regex.Pattern.quote(realname). It will change the pattern to a(1)(d+).txt.



    Thoughts:

    input file = a(2).txt to match a(2).txt and a(2)(1).txt you could remove fname.equals(name) and modify regex to fname.matches(Pattern.quote(realname) + "(\(\d+\))*\." + suffix). * means zero or more times inside round brackets; "." - dot is a special symbol and should be escaped.






    share|improve this answer




























      0














      You should learn debugging with IDE or at least printing values at sum checkpoint to narrow the source of issue.



      Your pattern for fname.matches() is a(1)(d+).txt. Round brackets are special symbols in regex to escape them you could use java.util.regex.Pattern.quote(realname). It will change the pattern to a(1)(d+).txt.



      Thoughts:

      input file = a(2).txt to match a(2).txt and a(2)(1).txt you could remove fname.equals(name) and modify regex to fname.matches(Pattern.quote(realname) + "(\(\d+\))*\." + suffix). * means zero or more times inside round brackets; "." - dot is a special symbol and should be escaped.






      share|improve this answer


























        0












        0








        0







        You should learn debugging with IDE or at least printing values at sum checkpoint to narrow the source of issue.



        Your pattern for fname.matches() is a(1)(d+).txt. Round brackets are special symbols in regex to escape them you could use java.util.regex.Pattern.quote(realname). It will change the pattern to a(1)(d+).txt.



        Thoughts:

        input file = a(2).txt to match a(2).txt and a(2)(1).txt you could remove fname.equals(name) and modify regex to fname.matches(Pattern.quote(realname) + "(\(\d+\))*\." + suffix). * means zero or more times inside round brackets; "." - dot is a special symbol and should be escaped.






        share|improve this answer













        You should learn debugging with IDE or at least printing values at sum checkpoint to narrow the source of issue.



        Your pattern for fname.matches() is a(1)(d+).txt. Round brackets are special symbols in regex to escape them you could use java.util.regex.Pattern.quote(realname). It will change the pattern to a(1)(d+).txt.



        Thoughts:

        input file = a(2).txt to match a(2).txt and a(2)(1).txt you could remove fname.equals(name) and modify regex to fname.matches(Pattern.quote(realname) + "(\(\d+\))*\." + suffix). * means zero or more times inside round brackets; "." - dot is a special symbol and should be escaped.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 5:18









        uliuli

        519313




        519313

























            0














            I think your regex should be



            ((d+))+.


            You also need to escape special character to use this regex in Java code as followed:



            (\(\d+\))+\.





            share|improve this answer
























            • The issue is with realname.

              – uli
              Nov 25 '18 at 4:56
















            0














            I think your regex should be



            ((d+))+.


            You also need to escape special character to use this regex in Java code as followed:



            (\(\d+\))+\.





            share|improve this answer
























            • The issue is with realname.

              – uli
              Nov 25 '18 at 4:56














            0












            0








            0







            I think your regex should be



            ((d+))+.


            You also need to escape special character to use this regex in Java code as followed:



            (\(\d+\))+\.





            share|improve this answer













            I think your regex should be



            ((d+))+.


            You also need to escape special character to use this regex in Java code as followed:



            (\(\d+\))+\.






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 25 '18 at 4:10









            thanh ngothanh ngo

            54939




            54939













            • The issue is with realname.

              – uli
              Nov 25 '18 at 4:56



















            • The issue is with realname.

              – uli
              Nov 25 '18 at 4:56

















            The issue is with realname.

            – uli
            Nov 25 '18 at 4:56





            The issue is with realname.

            – uli
            Nov 25 '18 at 4:56


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464460%2fcount-files-with-regex-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin