title: Java Regular Expression to Match Exact Word with Special Characters
body: <p>I have list of keywords entered by the user and they may contains the special characters like <code>$, #, @, ^, &amp;,</code> etc.</p>
  
  <p>As per my requirement when ever i receive list of text messages i need to search for all the keywords in every message.</p>
  
  <p>We need to <strong>match exact keyword</strong>.</p>
  
  <p><strong>CASE 1: Simple Keyword - Simple Message</strong></p>
  
  <p>I used <code>\b</code> to match exact keyword and it <strong><em>works</em></strong> fine.</p>
  
  <pre><code>public static void main(String[] args) {
          String patternStr =  "(?i)\\bHello\\b";
  
          Pattern pattern = Pattern.compile(patternStr);
  
          List&lt;String&gt; strList = new ArrayList&lt;String&gt;();
          strList.add("HHello Message");
          strList.add("This is Hello Message ");
          strList.add("Now Hellos again.");
  
          for(String str : strList) {
              Matcher matcher = pattern.matcher(str);
              System.out.println("&gt;&gt; "+matcher.find());
          }
      }
  </code></pre>
  
  <p><strong>OUTPUT as Expected</strong></p>
  
  <pre><code>&gt;&gt; false
  &gt;&gt; true
  &gt;&gt; false
  </code></pre>
  
  <p><strong>CASE 2 : Simple Keyword - Message with Special Character</strong></p>
  
  <p>Now, if i run above same code for following messages then it <strong><em>didn't work</em></strong> as expected.</p>
  
  <pre><code>List&lt;String&gt; strList = new ArrayList&lt;String&gt;();
  strList.add("#Hello Message");
  strList.add("This is Hello Message ");
  strList.add("Now Hellos again.");
  </code></pre>
  
  <p><strong>OUTPUT:</strong></p>
  
  <pre><code>true
  true
  false
  </code></pre>
  
  <p><strong>Expected OUTPUT</strong></p>
  
  <pre><code>false
  true
  false
  </code></pre>
  
  <p><strong>CASE 3 : Keyword &amp; Message with Special Character</strong></p>
  
  <p>If i receive following messages and Keyword is <code><strong>#Hello</strong></code>.
  I wrote following code but it <strong>didn't work</strong>.</p>
  
  <pre><code>public static void main(String[] args) {
          String patternStr =  "(?i)\\b#Hello\\b";
  
          Pattern pattern = Pattern.compile(patternStr);
  
          List&lt;String&gt; strList = new ArrayList&lt;String&gt;();
          strList.add("HHello Message");
          strList.add("This is #Hello Message ");
          strList.add("Now Hellos again.");
  
          for(String str : strList) {
              Matcher matcher = pattern.matcher(str);
              System.out.println("&gt;&gt; "+matcher.find());
          }
      }
  </code></pre>
  
  <p><strong>OUTPUT:</strong></p>
  
  <pre><code>&gt;&gt; false
  &gt;&gt; false
  &gt;&gt; false
  </code></pre>
  
  <p><strong>Expected OUTPUT:</strong></p>
  
  <pre><code>&gt;&gt; false
  &gt;&gt; true
  &gt;&gt; false
  </code></pre>
  
  <p>How can i escape the special characters and resolve<code> CASE 2 and CASE 3</code>.</p>
  
  <p>Please help.</p>