title: Java Regex and PathMatcher
body: <p>I'm writing an application in Java that displays a list of files where the first word in the filename matches user defined string, then deletes or rearranges them depending on some preferences. I'm currently in the phase of finding a good way to find my files. Using <a href="https://docs.oracle.com/javase/tutorial/essential/io/find.html" rel="nofollow">this Java Tutorial</a> I ended up with something like this:</p>
  
  <pre><code>Path source = Paths.get(sourceText.getText());
  Path dest = Paths.get(destText.getText());
  
  System.out.println("Source:" + source.toString());
  System.out.println("P/N: " + partNoText.getText());
  
  String matchString = "glob:**" + partNoText.getText() + "*";
  
  System.out.println("Matching: " + matchString);
  
  fileFinder = new FileFinder(matchString);
  
  try {
      Files.walkFileTree(source, fileFinder);
  } catch (IOException e1) {
      e1.printStackTrace();
  }
  for (Path path : fileFinder.getResult()) {
      System.out.println("Moving: " + path.getFileName());
      Path target = Paths.get(dest.toString() + "\\" + path.getFileName());
  
      try {
          Files.move(path, target, REPLACE_EXISTING);
      } catch (IOException e1) {
          e1.printStackTrace();
      }
  }
  </code></pre>
  
  <p>where FileFinder extends SimpleFileVisitor and has this visitFile method:</p>
  
  <pre><code>public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
      System.out.println(file.toString());
      System.out.println(fileMatcher.matches(file));
      if (fileMatcher.matches(file)) {
          result.add(file);
          return FileVisitResult.CONTINUE;
      }
      return FileVisitResult.CONTINUE;
  }
  </code></pre>
  
  <p>My issue with this is that the glob will pick up any file where the filename contains the part no. in any way. So if my file is called "12345 RevA Really Big Part 2: Electric Bugaloo" then the string would match if the user entered "1" or "123" or "Bugaloo". Ideally, it would only match if the user enterred "12345".</p>
  
  <p>I tried switching my matchString to <code>"regex: .*" + partNoText + "\\b"</code>, which works in the regex test harness I modified from <a href="https://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html" rel="nofollow">this other Java Tutorial</a>. What am I doing wrong? Does <code>PathMatcher</code> work differently than a regular <code>Matcher</code>?</p>
  
  <p>P.S. Any variable that has the word "Text" in it, like <code>sourceText</code> and <code>partNoText</code> are JTextFields. Hopefull that is the only part of the code that is mostly unclear from what I clipped out of it. </p>