R - 正则表达式中的精确匹配

时间:2017-05-21 21:22:11

标签: r regex

我在下次匹配时遇到问题。

假设我有下一个公式:

library(stringr)
str_extract_all("make thingsdo happn", "make|thin|happn|link|space")

[1] "make"  "thin"  "happn"

该函数正确返回单词“make”和“happn”,但不是“thin”。所以,我需要一个函数来返回与提供的列表完全匹配的每个单词。

我该如何解决?

1 个答案:

答案 0 :(得分:2)

您需要包含字词边界\b ...

str_extract_all("make thingsdo happn", "\\b(make|thin|happn|link|space)\\b")

[1] "make"  "happn"
相关问题