title: Identification of special characters in a string using R
body: <p>I have a data field which consists of firm names that may contain special characters such as @,/,-. I need to identify whether the data field contains any special characters. I have tried the suggestions listed on <a href="https://stackoverflow.com/questions/36928870/r-check-if-string-contains-special-characters">r check if string contains special characters</a>, <a href="https://stackoverflow.com/questions/27721008/how-do-i-deal-with-special-characters-like-in-my-regex">How do I deal with special characters like \^$.?*|+()[{ in my regex?</a> and <a href="https://stackoverflow.com/questions/45070117/r-check-if-special-character-in-string">R, check if special character in string</a> but they are not giving the correct results.</p>
  <p>The last two firm names should give a FALSE in the check field but none of the three approaches is yielding the right result. Please suggest how to correct my code. Thanks.</p>
  <pre><code>    df &lt;- data.frame(ID = c(1,2,3,4,5,6,7,8,9,10), Firm = c(&quot;Xi'an Feibao Technology Co Ltd&quot;, 
                                                 &quot;A&amp;B PVT LTD&quot;, &quot;Wonik Pne Co Ltd/Old&quot;,&quot;Wooree E&amp;L Co Ltd&quot;
                                                 , &quot;X-Fab Silicon Foundries SE&quot;, &quot;Yongsan S&amp;C&quot;, &quot;T-Gaia Corp&quot;,
                                                 &quot;Suntech Co Ltd/Seoul&quot;,&quot;IBM&quot;,&quot;31 Inc&quot;))
      
      df$nwords &lt;- str_count(df$Firm, &quot;\\w+&quot;)
      
      df$check1 &lt;- grepl('[^[:alnum:]]', df$Firm)
      
      df$check2 &lt;- grepl('[^[:punct:]]', df$Firm)
      
      pattern &lt;- &quot;/|:|\\?|&lt;|&gt;|\\|\\\\|\\|-|&amp;|'|*&quot;
      df$check3 &lt;- grepl(pattern, df$Firm)
  
  &gt; print(df)
     ID                           Firm nwords check1 check2 check3
  1   1 Xi'an Feibao Technology Co Ltd      6   TRUE   TRUE   TRUE
  2   2                    A&amp;B PVT LTD      4   TRUE   TRUE   TRUE
  3   3           Wonik Pne Co Ltd/Old      5   TRUE   TRUE   TRUE
  4   4              Wooree E&amp;L Co Ltd      5   TRUE   TRUE   TRUE
  5   5     X-Fab Silicon Foundries SE      5   TRUE   TRUE   TRUE
  6   6                    Yongsan S&amp;C      3   TRUE   TRUE   TRUE
  7   7                    T-Gaia Corp      3   TRUE   TRUE   TRUE
  8   8           Suntech Co Ltd/Seoul      4   TRUE   TRUE   TRUE
  9   9                            IBM      1  FALSE   TRUE   TRUE
  10 10                         31 Inc      2   TRUE   TRUE   TRUE
  </code></pre>