在R中的查找表中精确匹配字符串[重复]。

问题描述 投票:1回答:1

我有一个带有模式的查找值表,需要查找和替换,但模式中的字符串相互包含,我想将它们完全匹配。

lookup <- tibble(
  pattern = c("ONE", "ONET", "ONETR"),
  replacement = c("one new", "this is 2", "for 3")
)
other_table <- tibble(
  strings = c(
    "I want to replace ONE",
    "Or ONET is what to change",
    "We can change ONE again",
    "ONETR also can be replaced"
  ),
  other_dat = 1:4
)

我试过使用 stringi 但当这些模式相互包含时,这就不灵了。

other_table %>%
  mutate(
    strings = stringi::stri_replace_all_fixed(
      strings, 
      pattern = lookup$pattern, 
      replacement = lookup$replacement,
      vectorize_all = FALSE)
    )

我可以用什么函数来替换所有在 in_table$stringslookup$replacement?

理想的输出。

  strings                        other_dat
  <chr>                              <int>
1 I want to replace one new              1
2 Or this is 2 is what to change         2
3 We can change one new again            3
4 for 3 also can be replaced             4

感谢任何帮助!

r regex stringr stringi
1个回答
0
投票

在你的regex中使用字的边界(不是固定的),例如, "\\b".

other_table %>%
  mutate(
    strings = stringi::stri_replace_all(
      strings, 
      regex = paste0("\\b", lookup$pattern, "\\b"), 
      replacement = lookup$replacement,
      vectorize_all = FALSE)
    )
# # A tibble: 4 x 2
#   strings                        other_dat
#   <chr>                              <int>
# 1 I want to replace one new              1
# 2 Or this is 2 is what to change         2
# 3 We can change one new again            3
# 4 for 3 also can be replaced             4
© www.soinside.com 2019 - 2024. All rights reserved.