原文链接:http://www.sohu.com/a/205441283_466874
基础文本处理函数中正则表达式的应用
R中常用的支持正则表达式的基础文本处理函数包括grep/grepl、sub/gsub、regexpr/gregexpr等。
example_text1 <- c("23333#RRR#PP","35555#CCCC","louwill#2017")
#以#进行字符串切分
unlist(strsplit(example_text1, "#"))
[1] "23333" "RRR" "PP" "35555" "CCCC" "louwill" "2017"
#以空字符集进行字符串切分
unlist(strsplit(example_text1, "s"))
[1] "23333#RRR#PP" "35555#CCCC" "louwill#2017"
#以空字符替换字符串第一个#匹配
sub("#","", example_text1)
[1] "23333RRR#PP" "35555CCCC" "louwill2017"
#以空字符集替换字符串全部#匹配
gsub("#","",example_text1)
[1] "23333RRRPP" "35555CCCC" "louwill2017"
#查询字符串中是否存在3333或5555的特征并返回所在位置
grep("[35]{4}", example_text1)
[1] 1 2
#查询字符串中是否存在3333或5555的特征并返回逻辑值
grepl("[35]{4}", example_text1)
[1] TRUE TRUE FALSE
#返回匹配特征的字符串
pattern <- "[[:alpha:]]*(,|#)[[:alpha:]]"
m <- regexpr(pattern, example_text1)
regmatches(example_text1, m)
[1] "#R" "#C"
stringr包文本处理函数中的正则表达式的应用
stringr包一共为我们提供了30个字符串处理函数,其中大部分均可支持正则表达式的应用,包内所有函数均以str_开头,后面单词用来说明该函数的含义,相较于基础文本处理函数,stringr包函数更容易直观地理解。本文仅以str_extract和str_extract_all函数为例,对stringr包的正则表达式应用进行简要说明。
example_text2 <- "1. A small sentence. - 2. Another tiny sentence."
library(stringr)
#提取small特征字符
str_extract(example_text2, "small")
[1] "small"
#提取包含sentence特征的全部字符串
unlist(str_extract_all(example_text2, "sentence"))
[1] "sentence" "sentence"
#提取以1开始的字符串
str_extract(example_text2, "^1")
[1] "1"
#提取以句号结尾的字符
unlist(str_extract_all(example_text2, ".$"))
[1] "."
#提取包含tiny或者sentence特征的字符串
unlist(str_extract_all(example_text2, "tiny|sentence"))
[1] "sentence" "tiny" "sentence"
#点号进行模糊匹配
str_extract(example_text2, "sm.ll")
[1] "small"
#中括号内表示可选字符串
str_extract(example_text2, "sm[abc]ll")
[1] "small"
str_extract(example_text2, "sm[a-p]ll")
[1] "small"