title: Invalid regular expression in R using grep() with lookarounds
body: <p>I need to construct <code>regular expression</code> dynamically in order to perform an exact match in <code>R</code> using <code>grep()</code>. This is the code I have:</p>
  
  <pre><code>names = c('John Doe', 'John-Doe', 'Doe John', 'Doe-John', 'John', 'Doe')
  
  for(name in names) {
      pattern = paste('(?&lt;![A-z]-)^\\b', name, '\\b$(?!-[A-z])', sep = '')
      index = grep(pattern, names)
  
      print(index)
  }
  </code></pre>
  
  <p>Desired output:</p>
  
  <ul>
  <li>each name must be match exactly to an element within the <code>names</code> vector</li>
  <li>e.g., <code>John</code> should return only <code>index</code> <code>5</code> and nothing else</li>
  </ul>
  
  <p>I tested my <code>regular expression</code> here <a href="https://regex101.com/r/uJhJwS/2" rel="nofollow noreferrer">https://regex101.com/r/uJhJwS/2</a> and it appears to work fine. However, I get the following error in <code>R</code>:</p>
  
  <pre><code>Error in grep(pattern, names) : 
    invalid regular expression '(?&lt;![A-z]-)John Do$(?!-[A-z])', reason 'Invalid regexp'
  </code></pre>
  
  <p>What is going wrong?</p>