I have a directory with thousands of files that follow a finite number of different naming conventions. I am trying to use RegEx to help me identify the file naming conventions as I iterate over the files name list. I will then use this logic to choose the correct file name parsing methodology.
This is my code:
//Define regex rule <br>
String regExRule = "Final_[0-9]{1,4}-[0-9]{1,4}([.][0-9]{1,3})_[A-Za-z0-9]+([.][0-9]{1,3})?[.]wav";
//Compile the rule <br>
Pattern p = Pattern.compile(regExRule);
//See if there is a match <br>
Matcher m = p.matcher("Final_0-1.1_FirstLast.4.wav");
//See if there is a match <br>
if(m.matches()){ //we have a match}
// Always returns false even though I can confirm the rule works using
//this tool: https://www.regextester.com/
Rule: <br>
Final_[0-9]{1,4}-[0-9]{1,4}([.][0-9]{1,3})_[A-Za-z0-9]+([.][0-9]{1,3})?[.]wav
Test String:
Final_0-1.1_FirstLast.4.wav
Your original expression appears to be working well, if I understand the problem right, we would just then replace [.]
with \\.
, if you wish:
Final_[0-9]{1,4}-[0-9]{1,4}(\.[0-9]{1,3})_[A-Za-z0-9]+([.][0-9]{1,3})?\.wav
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "Final_[0-9]{1,4}-[0-9]{1,4}(\\.[0-9]{1,3})_[A-Za-z0-9]+([.][0-9]{1,3})?\\.wav";
final String string = "Final_0-1.1_FirstLast.4.wav";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
jex.im visualizes regular expressions:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.