-2

I might be missing something very obvious but how can I write efficient code to get all matches of a singular version of a noun but NOT its plural? for example, I want to match

angel investor

angel

BUT NOT

angels

try angels

If I try

grep("angel ", string)

Then a string with JUST the word angel won't match.

Please help!

Toto
  • 89,455
  • 62
  • 89
  • 125
Malvika
  • 61
  • 6

2 Answers2

0

You can try the following approach. It still believe there are other excellent ways to solve this problem.

df <- data.frame(obs = 1:4, words = c("angle", "try angles", "angle investor", "angles"))

df %>% 
  filter(!str_detect(words, "(?<=[ertkgwmnl])s\\b"))
#     obs        words
# 1   1          angle
# 2   3 angle investor
Tho Vu
  • 1,304
  • 2
  • 8
  • 20
0

Use word-boundary markers \\b:

x <- c("angel investor", "angel","angels", "try angels")

grep("\\bangel\\b", x, value = T)
[1] "angel investor" "angel" 
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34