 <p>You seem to be looking for <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/grep.html" rel="nofollow"><code>invert</code></a>:</p> <blockquote> <p><code>invert</code> logical. If <code>TRUE</code> return indices or values for elements that do <em>not</em> match.</p> </blockquote> <pre><code>&gt; id.data = c("XXX-2355", + "XYz-03", + "XYU-3", + "ABC-1234", + "AX_2356", + "AbC234") &gt; grep("[A-Z]{3}-[0-9]{4}", id.data) [1] 1 4 &gt; grep("[A-Z]{3}-[0-9]{4}", id.data, value = TRUE) [1] "XXX-2355" "ABC-1234" &gt; grep("[A-Z]{3}-[0-9]{4}", id.data, invert = TRUE) [1] 2 3 5 6 &gt; grep("[A-Z]{3}-[0-9]{4}", id.data, invert = TRUE, value = TRUE) [1] "XYz-03" "XYU-3" "AX_2356" "AbC234" &gt; </code></pre> <p><sub>Not sure whether you want strings that match the said pattern, or those that don't match. The above example lists both options.</sub></p>