Apostrophes and optional argument (?) in grep vs agrep

156 views Asked by At

When I run the below 4 lines of code I dont get the same result from all 4. Why is the last line not finding a match?

grep("CPA's", c("CPA's"))
agrep("CPA's", c("CPA's"))

grep("CPA'?s?", c("CPA's"))
agrep("CPA'?s?", c("CPA's"))

I haven't yet done my full reading on the fuzzy matching functions, but on the face of it I don't see why this would be an issue.

1

There are 1 answers

2
Ronak Shah On

Because the difference is more than the default max.distance which is 0.1. Increase the max.distance and it will capture it.

agrep("CPA'?s?", "CPA's", max.distance = 0.15)
#[1] 1

To treat pattern as regular expression, select fixed = FALSE which will then work directly

agrep("CPA'?s?", "CPA's", fixed = FALSE)