title: Java match whole word in String
body: <p>I have an <code>ArrayList&lt;String&gt;</code> which I iterate through to find the correct index given a String. Basically, given a String, the program should search through the list and find the index where the whole word matches. For example:</p>
  
  <pre><code>ArrayList&lt;String&gt; foo = new ArrayList&lt;String&gt;();
  foo.add("AAAB_11232016.txt");
  foo.add("BBB_12252016.txt");
  foo.add("AAA_09212017.txt");
  </code></pre>
  
  <p>So if I give the String <code>AAA</code>, I should get back index <code>2</code> (the last one). So I can't use the <code>contains()</code> method as that would give me back index <code>0</code>. </p>
  
  <p>I tried with this code:</p>
  
  <pre><code>String str = "AAA";
  String pattern = "\\b" + str + "\\b";
  Pattern p = Pattern.compile(pattern);
  
  for(int i = 0; i &lt; foo.size(); i++) {
      // Check each entry of list to find the correct value
      Matcher match = p.matcher(foo.get(i));
  
      if(match.find() == true) {
          return i;
      }
  }
  </code></pre>
  
  <p>Unfortunately, this code never reaches the <code>if</code> statement inside the loop. I'm not sure what I'm doing wrong.</p>
  
  <p>Note: This should also work if I searched for <code>AAA_0921</code>, the full name <code>AAA_09212017.txt</code>, or any part of the String that is unique to it.</p>