使用N种模式中str_replace函数中的R?

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

由于我有N不同的模式我想str_replace检查每个模式:

pat_list <- pattern1|pattern2| ......|patternN

str_replace(string, pattern = pat_list)

比如这里有我的方式:

[1] "123-5"   "123-05"  "123-1"   "39-33"   "05"      "44-078"  "31-6"    "972-11"  "45-"     "33-7"    "49-17"   "20-12"   "123-"    "User_52" "44-79"   "33-6"   
[17] "44-75"   "358-4"   "43-699"  NA        "" 

现在我该怎样“告诉” str_replace使用的模式列表?

r regex stringr
1个回答
0
投票

也许你需要的是

patterns <- c("123-5", "123-05", "123-1")
str_replace_all("123123-50123-0533", paste(patterns, collapse = "|"), "")
# [1] "123033"
© www.soinside.com 2019 - 2024. All rights reserved.