count files with regex in java
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
add a comment |
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
Typo: liatFiles
– Pavitra
Nov 25 '18 at 3:55
add a comment |
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
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
java regex
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
I think your regex should be
((d+))+.
You also need to escape special character to use this regex in Java code as followed:
(\(\d+\))+\.
The issue is withrealname
.
– uli
Nov 25 '18 at 4:56
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 25 '18 at 5:18
uliuli
519313
519313
add a comment |
add a comment |
I think your regex should be
((d+))+.
You also need to escape special character to use this regex in Java code as followed:
(\(\d+\))+\.
The issue is withrealname
.
– uli
Nov 25 '18 at 4:56
add a comment |
I think your regex should be
((d+))+.
You also need to escape special character to use this regex in Java code as followed:
(\(\d+\))+\.
The issue is withrealname
.
– uli
Nov 25 '18 at 4:56
add a comment |
I think your regex should be
((d+))+.
You also need to escape special character to use this regex in Java code as followed:
(\(\d+\))+\.
I think your regex should be
((d+))+.
You also need to escape special character to use this regex in Java code as followed:
(\(\d+\))+\.
answered Nov 25 '18 at 4:10
thanh ngothanh ngo
53939
53939
The issue is withrealname
.
– uli
Nov 25 '18 at 4:56
add a comment |
The issue is withrealname
.
– 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Typo: liatFiles
– Pavitra
Nov 25 '18 at 3:55