本文转自微信公众号: 一遇之见 的 大作 R中字符串处理:函数实现 。原文太长,分三次学习、消化。
字符串匹配查询
函数match
、grep
,grepl
,str_detect
,str_locate
和str_locate_all
,str_match
和str_match_all
均可实现字符串的匹配查询,但有略有不同。函数match
可实现多对多的完全匹配,但同一个值仅能返回到第一次匹配到的位置索引值;其它函数则可实现完全或者部分匹配。
match
function
match("cd", c("ab", "cd", "ef", "mn")) # 完全匹配
# [1] 2
match("cd", c("ab", "cdx", "ef", "mn")) # 部分匹配不能实现
# [1] NA
match("cd", c("ab", "cd", "ef", "cd", "mn")) # 仅返回第一次被匹配到的位置向量
# [1] 2
match(c("cd", "mn"), c("ab", "cd", "ef", "mn"))
# [1] 2 4
match(c("cd", "mn"), c("ab", "cd", "ef", "cd", "mn"))
# [1] 2 5
grep
and grepl
function
grep("cd", c("cxcx", "mn", "mn", "cd", "ef", "mn", "cxcx")) # 完全匹配
# [1] 4
grep("cd", c("cxcx", "mn", "mn", "cdxcdx", "ef", "mn", "cxcx")) # 部分匹配
# [1] 4
grep("cd", c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx")) # 一对多完全或部分匹配查询,有几个匹配几个
# [1] 1 4 7
grep(c("cd", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx")) # 多对多完全或部分匹配查询,其实只有第一个"cd"参与了匹配
# [1] 1 4 7 # Warning message:only the first element will be used
grepl("cd", c("cxcx", "mn", "mn", "cd", "ef", "mn", "cxcx")) # 完全匹配
# [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE
grepl("cd", c("cxcx", "mn", "mn", "cdxcdx", "ef", "mn", "cxcx")) # 部分匹配
# [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE
grepl("cd", c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx")) # 一对多完全或部分匹配,有几个匹配几个
# [1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE
grepl(c("cd", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx")) # 多对多完全或部分匹配查询,其实只有第一个"cd"参与了匹配
# [1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE # Warning message: only the first element will be used
str_detect
function
str_detect(c("cd", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"))
# [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
# Warning message:
# In stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_detect(c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"), c("cd", "mn"))
# [1] TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
# Warning message:
# In stri_detect_regex(string, pattern, negate = negate, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_locate
and str_locate_all
function
str_locate(c("cd", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"))
# start end
# [1,] NA NA
# [2,] NA NA
# [3,] NA NA
# [4,] NA NA
# [5,] NA NA
# [6,] NA NA
# [7,] NA NA
# [8,] 1 2
# [9,] 1 2
# Warning message:
# In stri_locate_first_regex(string, pattern, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_locate(c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"), c("cd", "mn"))
# start end
# [1,] 1 2
# [2,] 1 2
# [3,] NA NA
# [4,] NA NA
# [5,] NA NA
# [6,] 1 2
# [7,] 1 2
# [8,] 1 2
# [9,] 1 2
# Warning message:
# In stri_locate_first_regex(string, pattern, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_locate_all(c("cd", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"))
# [[1]]
# start end
#
# [[2]]
# start end
#
# [[3]]
# start end
#
# [[4]]
# start end
#
# [[5]]
# start end
#
# [[6]]
# start end
#
# [[7]]
# start end
#
# [[8]]
# start end
# [1,] 1 2
#
# [[9]]
# start end
# [1,] 1 2
#
# Warning message:
# In stri_locate_all_regex(string, pattern, omit_no_match = TRUE, :
# longer object length is not a multiple of shorter object length
str_locate_all(c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"), c("cd", "mn"))
# [[1]]
# start end
# [1,] 1 2
# [2,] 4 5
#
# [[2]]
# start end
# [1,] 1 2
#
# [[3]]
# start end
#
# [[4]]
# start end
#
# [[5]]
# start end
#
# [[6]]
# start end
# [1,] 1 2
#
# [[7]]
# start end
# [1,] 1 2
# [2,] 4 5
#
# [[8]]
# start end
# [1,] 1 2
#
# [[9]]
# start end
# [1,] 1 2
#
# Warning message:
# In stri_locate_all_regex(string, pattern, omit_no_match = TRUE, :
# longer object length is not a multiple of shorter object length
str_match
and str_match_all
function (in stringr
package)
str_match(c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"), c("cdx", "mn"))
# [,1]
# [1,] "cdx"
# [2,] "mn"
# [3,] NA
# [4,] NA
# [5,] NA
# [6,] "mn"
# [7,] "cdx"
# [8,] "mn"
# [9,] NA
# Warning message:
# In stri_match_first_regex(string, pattern, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_match(c("cdx", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"))
# [,1]
# [1,] NA
# [2,] NA
# [3,] NA
# [4,] NA
# [5,] NA
# [6,] NA
# [7,] NA
# [8,] "mn"
# [9,] "cd"
# Warning message:
# In stri_match_first_regex(string, pattern, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_match_all(c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"), c("cdx", "mn"))
# [[1]]
# [,1]
# [1,] "cdx"
# [2,] "cdx"
#
# [[2]]
# [,1]
# [1,] "mn"
#
# [[3]]
# [,1]
#
# [[4]]
# [,1]
#
# [[5]]
# [,1]
#
# [[6]]
# [,1]
# [1,] "mn"
#
# [[7]]
# [,1]
# [1,] "cdx"
# [2,] "cdx"
#
# [[8]]
# [,1]
# [1,] "mn"
#
# [[9]]
# [,1]
#
# Warning message:
# In stri_match_all_regex(string, pattern, omit_no_match = TRUE, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_match_all(c("cdx", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"))
# [[1]]
# [,1]
#
# [[2]]
# [,1]
#
# [[3]]
# [,1]
#
# [[4]]
# [,1]
#
# [[5]]
# [,1]
#
# [[6]]
# [,1]
#
# [[7]]
# [,1]
#
# [[8]]
# [,1]
# [1,] "mn"
#
# [[9]]
# [,1]
# [1,] "cd"
#
# Warning message:
# In stri_match_all_regex(string, pattern, omit_no_match = TRUE, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_extract
and str_extract_all
function
str_extract(c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"), c("cdx", "mn"))
# [1] "cdx" "mn" NA NA NA "mn" "cdx" "mn" NA
# Warning message:
# In stri_extract_first_regex(string, pattern, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_extract(c("cdx", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"))
# [1] NA NA NA NA NA NA NA "mn" "cd"
# Warning message:
# In stri_extract_first_regex(string, pattern, opts_regex = opts(pattern)) :
# longer object length is not a multiple of shorter object length
str_extract_all(c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"), c("cdx", "mn"))
# [[1]]
# [1] "cdx" "cdx"
#
# [[2]]
# [1] "mn"
#
# [[3]]
# character(0)
#
# [[4]]
# character(0)
#
# [[5]]
# character(0)
#
# [[6]]
# [1] "mn"
#
# [[7]]
# [1] "cdx" "cdx"
#
# [[8]]
# [1] "mn"
#
# [[9]]
# character(0)
#
# Warning message:
# In stri_extract_all_regex(string, pattern, simplify = simplify, :
# longer object length is not a multiple of shorter object length
str_extract_all(c("cdx", "mn"), c("cdxcdx", "mny", "mn", "cd", "ef", "mnz", "cdxcdx", "mn", "cd"))
# [[1]]
# character(0)
#
# [[2]]
# character(0)
#
# [[3]]
# character(0)
#
# [[4]]
# character(0)
#
# [[5]]
# character(0)
#
# [[6]]
# character(0)
#
# [[7]]
# character(0)
#
# [[8]]
# [1] "mn"
#
# [[9]]
# [1] "cd"
#
# Warning message:
# In stri_extract_all_regex(string, pattern, simplify = simplify, :
# longer object length is not a multiple of shorter object length
原文参考文章:
1.字符串处理
2.R语言字符串处理包stringr