stringr-----str_match
主页:https://cran.r-project.org/web/packages/stringr/index.html
#安装stringr包> install.packages('stringr')> library(stringr)
#stringr函数分类:
字符串拼接函数
字符串计算函数
字符串匹配函数
字符串变换函数
参数控制函数
#stringr字符串匹配函数
str_match(string, pattern) str_match_all(string, pattern)
string: 字符串,字符串向量。 pattern: 匹配字符。
从字符串中提取匹配组
> val <- c("abc", 123, "cba") # 匹配字符a,并返回对应的字符 > str_match(val, "a") [,1] [1,] "a" [2,] NA [3,] "a" # 匹配字符0-9,限1个,并返回对应的字符 > str_match(val, "[0-9]") [,1] [1,] NA [2,] "1" [3,] NA # 匹配字符0-9,不限数量,并返回对应的字符 > str_match(val, "[0-9]*") [,1] [1,] "" [2,] "123" [3,] ""
从字符串中提取匹配组,以字符串matrix格式返回
> str_match_all(val, "a") [[1]] [,1] [1,] "a" [[2]] [,1] [[3]] [,1] [1,] "a" > str_match_all(val, "[0-9]") [[1]] [,1] [[2]] [,1] [1,] "1" [2,] "2" [3,] "3" [[3]] [,1]
赞 (0)