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