1

I am having some difficulty trying to create a working regex pattern for recognizing a string of file names.

Pattern pattern = Pattern.compile("(\\w+)(\\.)(t)(x)(t)(\\s$|\\,)");

When using a .find() from a matcher class on my sample input, say

"file1.txt,file2.txt "

I am returned true, which is fine, however other erroneous input also returns true.

This erroneous input includes strings such as:

"file1.txt,,file2.txt "

"file%.text " 

I have been consulting this website as I have been trying to construct them, I'm pretty sure I am missing something rather obvious though. Link

5
  • Please post your sample input and expected output. Commented Sep 19, 2014 at 13:12
  • "I'm not quite getting the results I would like." which are...? Commented Sep 19, 2014 at 13:16
  • @user1803551, I should have explained myself a bit more for the first input listed, I would like it to return true, for the two secondary examples, those should return false. Commented Sep 19, 2014 at 13:17
  • I guess you are trying to get .txt extensions, one at least, and the others separated by commas. Try this ([A-Za-z0-9]+\\.txt)+(,[A-Za-z0-9]+\\.txt)* Commented Sep 19, 2014 at 13:17
  • Try an online regex checker... to learn how to get it right... Commented Sep 19, 2014 at 13:18

4 Answers 4

2

To validate your file name list, you can use the following solution:

//                                  | preceded by start of input, or
//                                  |    | comma, preceded itself by either word or space
//                                  |    |              | file name
//                                  |    |              |     | dot
//                                  |    |              |     |    | extension
//                                  |    |              |     |    |  | optional 1 space
//                                  |    |              |     |    |  |   | followed by end of input
//                                  |    |              |     |    |  |   |     | or comma, 
//                                  |    |              |     |    |  |   |     | followed itself by word character
Pattern pattern = Pattern.compile("(?<=^|(?<=[\\w\\s]),)(\\w+)(\\.)txt\\s?(?=$|,(?=\\w))");
String input = "file1.txt,file2.txt";
String badInput = "file3.txt,,file4.txt";
String otherBadInput = "file%.txt, file!.txt";
Matcher m = pattern.matcher(input);
while (m.find()) {
    // printing group 1: file name
    System.out.println(m.group(1));
}
m = pattern.matcher(badInput);
// won't find anything
while (m.find()) {
    // printing group 1: file name
    System.out.println(m.group(1));
}
m = pattern.matcher(otherBadInput);
// won't find anything
while (m.find()) {
    // printing group 1: file name
    System.out.println(m.group(1));
}

Output

file1
file2
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the thorough explanation of the expression.
2

May this help you:

Pattern pattern = Pattern.compile("(.*?\\.\\w+)(?:,|$)");
String files = "file1.txt,file2.txt";
Matcher mFile = pattern.matcher(files);
while (mFile.find()) {
    System.out.println("file: " + mFile.group(1));
}

Output:

file: file1.txt
file: file2.txt

With only .txt files: (.*?\\.txt)(?:,|$)

1 Comment

This expression gave me the results I was looking for, thank you very much @Nicolas.
1

Naive way :

    public boolean validateFileName(String string){
      String[] fileNames= string.split(",");
      Pattern pattern = Pattern.compile("\b[\w]*[.](txt)\b"); /*match complete name, not just pattern*/
      for(int i=0;i<fileNames.length;i++){
          Matcher m = p.matcher(fileNames[i]);
          if (!m.matches())
              return false;
      }
      return true;
    }

Comments

1
Pattern p = Pattern.compile("((\\w+\\.txt)(,??|$))+");

The above Pattern lets "file1.txt,,file2.txt" pass, but does not get an empty file for the two commas. The other Strings "file1.txt,file2.txt" and "file%txt" are processed correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.