當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R stringr invert_match 將匹配位置切換到非匹配位置

反轉匹配位置矩陣以匹配先前匹配的相反內容。

用法

invert_match(loc)

參數

loc

匹配位置矩陣,來自str_locate_all()

數字匹配給出不匹配的位置

例子

numbers <- "1 and 2 and 4 and 456"
num_loc <- str_locate_all(numbers, "[0-9]+")[[1]]
str_sub(numbers, num_loc[, "start"], num_loc[, "end"])
#> [1] "1"   "2"   "4"   "456"

text_loc <- invert_match(num_loc)
str_sub(numbers, text_loc[, "start"], text_loc[, "end"])
#> [1] ""      " and " " and " " and " ""     
源代碼:R/locate.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Switch location of matches to location of non-matches。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。