这篇博客是对R语言中的正则表达式进行的整理,以实际应用为主,主要用于学习备忘,后续会根据需要进行补充。常用函数部分仅给出部分示例,详细使用方式可以使用R的帮助函数查询。
1、语法规则
常用符号及含义
符号 | 含义 |
---|---|
\ | 转义字符 |
. | 除了换行以外的任意字符 |
^ | 放在句首,表示一行字符串的起始 |
$ | 放在句尾,表示一行字符串的结束 |
* | 零个或者多个之前的字符 |
+ | 一个或者多个之前的字符 |
? | 零个或者一个之前的字符 |
[] | 匹配其中任何一个字符,而^在[]中代表“非”, -代表“之间” |
| | 或者,小括号()与花括号{}配合“|”使用 |
常用特殊转义字符含义
转义字符 | 含义 |
---|---|
\n | 换行符 |
\t | 制表符 |
\w | 任意字母(包括下划线)或者数字 即 [a-zA-Z0-9_] |
\W | \w的反义 即[^a-zA-Z0-9_] |
\d | 任意一个数字 即[0-9] |
\D | \d的反义 即[^0-9] |
\s | 任意一个空格,比如space, tab, newline 等 |
\S | \s的反义,任意一个非空格 |
2、常用函数
2.1 grep和grepl函数
这两个函数返回向量水平的匹配结果,不涉及匹配字符串的详细位置信息。其中,grep仅返回匹配项的下标,而grepl返回所有的查询结果,并用逻辑向量表示有没有找到匹配。两者的结果用于提取数据子集的结果都一样。
示例:列出C:\windows目录下的所有文件,然后用grep和grepl查找exe文件
> files <- list.files("c:/windows")
> grep("\\.exe$", files)
[1] 8 28 30 35 36 57 68 98 99 101 110
> grepl("\\.exe$", files)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
[11] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> files[grep("\\.exe$", files)]
[1] "bfsvc.exe" "explorer.exe" "fveupdate.exe" "HelpPane.exe"
[5] "hh.exe" "notepad.exe" "regedit.exe" "twunk_16.exe"
[9] "twunk_32.exe" "uninst.exe" "winhelp.exe"
> files[grepl("\\.exe$", files)]
[1] "bfsvc.exe" "explorer.exe" "fveupdate.exe" "HelpPane.exe"
[5] "hh.exe" "notepad.exe" "regedit.exe" "twunk_16.exe"
[9] "twunk_32.exe" "uninst.exe" "winhelp.exe"
2.2 regexpr、gregexpr和regexec
这三个函数返回的结果包含了匹配的具体位置和字符串长度信息,可以用于字符串的提取操作。
> text <- c("Hellow, Adam!", "Hi, Adam!")
> regexpr("Adam", text)
[1] 9 5
attr(,"match.length")
[1] 4 4
attr(,"useBytes")
[1] TRUE
> gregexpr("Adam", text)
[[1]]
[1] 9
attr(,"match.length")
[1] 4
attr(,"useBytes")
[1] TRUE
[[2]]
[1] 5
attr(,"match.length")
[1] 4
attr(,"useBytes")
[1] TRUE
> regexec("Adam", text)
[[1]]
[1] 9
attr(,"match.length")
[1] 4
[[2]]
[1] 5
attr(,"match.length")
[1] 4
2.3 sub和gsub
sub和gsub的区别是前者只做一次替换(不管有几次匹配),而gsub把满足条件的匹配都做替换。虽然sub和gsub是用于字符串替换的函数,但严格地说R语言没有字符串替换的函数,因为R语言不管什么操作对参数都是传值不传址。
> sub(pattern="Adam|Ava", replacement="world", text)
[1] "Hello world!\nHello Ava!"
> gsub(pattern="Adam|Ava", replacement="world", text)
[1] "Hello world!\nHello world!"
3、stringr包中实现正则表达的函数
3.1 str_detect
str_detect函数用于检测字符向量中是否存在匹配模式,返回逻辑值。
> some_objs = c("pen", "pencil", "marker", "spray")
> str_detect(some_objs, "pen")
[1] TRUE TRUE FALSE FALSE
3.2 str_extract和str_extract_all
str_extract函数的功能是剥离文本向量中每个元素中哦哦那个第一个匹配的值,无匹配的元素返回NA,结果是一个和文本向量同等维度的向量;str_extract_all功能类似str_extract,但是它会剥离文本向量中每个元素中所有符合匹配模式的值,无匹配时返回character(0),结果格式为list格式。
> paris_tweets = c("#Paris is chock-full of cultural and culinary attractions", "Some time in #Paris along Canal St.-Martin famous by #Amelie", "While you're in #Paris, stop at cafe: http://goo.gl/yaCbW", "Paris, the city of light")
> hash = "#[a-zA-Z]{1,}"
> str_extract(paris_tweets, hash)
[1] "#Paris" "#Paris" "#Paris" NA
3.3 str_match和str_match_all
较str_extract和str_extract_all,str_match和str_match_all会给出详细的匹配信息。
> str_match(strings, dates)
[,1] [,2] [,3] [,4]
[1,] "12 Jun 2002" "12" "Jun" "2002"
[2,] "8 September 2004" "8" "September" "2004"
[3,] "22-July-2009" "22" "July" "2009"
[4,] NA NA NA NA
... ...
> str_match_all(paris_tweets, hash)
[[1]]
[,1]
[1,] "#Paris"
[[2]]
... ...
3.4 str_locate和str_locate_all
这两个函数是用于定位匹配模式在文本向量每个元素中的位置,同上前者只定位第一个出现的匹配模式,后者针对所有匹配的模式;str_locate返回的是两列的矩阵,str_locate_all返回的是list。
> str_locate(paris_tweets, hash)
start end
[1,] 1 6
[2,] 14 19
[3,] 17 22
[4,] NA NA
> str_locate_all(paris_tweets, hash)
[[1]]
start end
[1,] 1 6
[[2]]
... ...
3.5 str_replace和str_replace_all
这两个函数用于替换,用指定模式替换匹配模式。
> cities = c("San Francisco", "Barcelona", "Naples", "Paris")
> str_replace(cities, "[aeiou]", ";")
[1] "S;n Francisco" "B;rcelona" "N;ples" "P;ris"
> str_replace_all(cities, pattern = "[aeiou]", ";")
[1] "S;n Fr;nc;sc;" "B;rc;l;n;" "N;pl;s" "P;r;s"
3.6 str_split和str_split_fixed
str_split语法形如: str_split(string, pattern, n=Inf),其中n是返回的分割数目;str_split_fixed函数同样用于分割,其语法格式为:str_split_fixed(string, pattern, n),这里n没有默认值,必须指定。
> sentence = c("R is a collaborative project with many contributors")
> str_split(sentence, " ")
[[1]]
[1] "R" "is" "a" "collaborative"
[5] "project" "with" "many" "contributors"
> flavors = c("chocolate", "vanilla", "cinnamon", "mint", "lemon")
# 以n为界分割为两部分
> str_split_fixed(flavors, "n", 2)
[,1] [,2]
[1,] "chocolate" ""
[2,] "va" "illa"
[3,] "ci" "namon"
[4,] "mi" "t"
[5,] "lemo" ""
参考链接:
http://www.thebigdata.cn/JiShuBoKe/13826.html
http://blog.163.com/yugao1986@126/blog/static/692285082014325113834380/
http://blog.163.com/shen_960124/blog/static/60730984201411184587551/