0

What I'm trying to do is look from a list of files in a directory and see which file name has the pattern 'output'. If the file does contain the word 'output' then I just want to print it to screen. That's it.

Here is my code that doesn't work. Why doesn't it work?

package duplicate_search;

import java.io.File;
import java.util.regex.*;
import java.util.Scanner;

public class Search {
public static void main(String [] args){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the directory to search: ");
    String dir = keyboard.next();

    Pattern p = Pattern.compile("output");
    Matcher m = null;
    System.out.println
              ("Now search for *." + p + " files.");
    File folder = new File(dir);
    File[] listOfFiles = folder.listFiles();


    for(File f : listOfFiles){
        m = p.matcher(f.getName());
        if(m.matches()){
            System.out.println(f.getName());
        }
      }
    }
}
2
  • What output are you getting, and from what input?
    – algorowara
    Aug 1, 2012 at 15:32
  • Output is: Enter the directory to search: /media/DATA/data_sets Now search for *.output files.
    – Erik
    Aug 1, 2012 at 16:21

4 Answers 4

3

If you want to match filenames that contain "output", then you don't want to use matches, but instead, find.

From the docs:

The matches method attempts to match the entire input sequence against the pattern.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Matcher.html

3

.matches() does a full match. check for .*output.* or use find()

2
  • 1
    or to avoid regex use indexOf(String) and check if it returns -1 or position index of found word like "file name with output.txt".indexOf("output")>-1
    – Pshemo
    Aug 1, 2012 at 15:37
  • @Pshemo true. regex is used in many cases were simple String matching would do fine Aug 1, 2012 at 15:43
1

Why aren't you using contains() method? Unless I'm not getting your question right you can check if a string contains that character sequence:

f.getName().contains("output"); //Returns true if the string contains "output"

IMHO a regular expression seems like an overkill here, unless you have your reasons of course.

2
  • It's a valid answer. You're searching a String for a single substring with no wildcards or repetitions, and doing so quickly and efficiently with this method.
    – algorowara
    Aug 1, 2012 at 16:28
  • I just wanted to expand your options with a simple suggestion. as I added, IMHO a regular expression seems like an overkill in this particular case. Plus, I'm not repeating what other person answered perfectly clear before (I'm talking about Sean's answer), repeating other person's answer has no merit.
    – Fritz
    Aug 1, 2012 at 17:30
0

Here is the modified code,

import java.io.File;
import java.util.regex.*;
import java.util.Scanner;

public class Search {
public static void main(String [] args){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the directory to search: ");
    String dir = keyboard.next();

    Pattern p = Pattern.compile("output");
    Matcher m = null;
    System.out.println
              ("Now search for *." + p + " files.");
    File folder = new File(dir);
    File[] listOfFiles = folder.listFiles();


    for(File f : listOfFiles){
        m = p.matcher(f.getName());
        if(m.find()){ // change to find instead of m.matches()
            System.out.println(m.group()); // return the string sequence which is matched
        }
      }
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.