Regular Expression
R语言之正则表达式 -----------------------------------------------------------------------------
Ⅰ. 用途
①查找特定信息,也就是提取 ②查找并编辑特定的信息,也就是替换
Ⅱ. R中使用正则表达式进行匹配的常用函数
gerp(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
fixed = FALSE, useBytes = FALSE, invert = FALSE) # 查找,返回结果是匹配项的下标
grepl(pattern, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE) # 查找,返回值为true
sub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE) # 只对查找到的第一个内容进行替换。
gsub(pattern, replacements, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE) # 对查找到的所有内容进行替换,
#返回替换后的text;否则直接返回text
regexpr(pattern, text, ignore.case = FALSE, per = FALSE,
fixed = FALSE, useBytes = FALSE) #返回第一个特征匹配的位置(对于每一个字符串)
gregexpr(pattern, x) # 返回所有特征匹配的位置(对于每一个字符串)
regmatches(x, m) # 提取匹配特征的第一个/所有字符串(对于每一个字符串),
# m = regexpr()/gregexpr()
library(stringr)
str_extract(text, pattern) # 提取匹配特征的第一个字符串(对于每一个字符串)
str_extract_all(text,pattern)# 提取匹配特征的所有字符串(对于每一个字符串)
对参数进行解释说明:
Parameters | Explanations |
---|---|
pattern | regular expression |
x(text) | character vector or character object, single str number limit = 231 |
ignore.case | 默认FALSE,表示区分大小写,TRUE时表示不区分大小写。 |
perl | 是否使用perl兼容的正则表达式。 |
value | 默认FALSE, 当查找到时返回1, 否则返回0; 当为TRUE查找到时返回匹配到的字符, 否则返回0。 |
fixed | 如果为TRUE, pattern是要匹配的字符串,覆盖所有冲突的参数。 |
useBytes | 默认为FALSE, 当为TRUE时,则是逐字节逐字节匹配而不是逐字符逐字符匹配。 |
invert | 如果TRUE返回不匹配的元素的索引或值。 |
replacement | 如果查找到之后,进行替换,若没有找到,则返回x(text)。 |
Ⅲ. 正则表达式字符
字符 | 描述 |
---|---|
[\b] | 空白元字符 , 回退(并删除)一个字符(backspace) |
\f | 换页符 |
\n | 换行符 |
\t | 制表符 |
\v | 垂直制表符 |
\d | 匹配数字,等价于[0-9] |
\D | 匹配非数字,等价于 ^[0-9] |
\w | 匹配任何一个字母、数字、下划线字符, 等价于[a-zA-Z0-9] |
\W | 匹配任何一个非字母数字或下划线字符, 等价于^[a-zA-Z0-9] |
\s | 空白字符 |
\S | 任何一个非空白字符 |
[:lower:] | 小写字母,等价于[a-z] |
[:upper:] | 大写字母,等价于[A-Z] |
\\ | 匹配转义字符 |
| | 表示或, 即| 前后表达式任选一个 |
^ | 匹配字符串的开始,如果置于[]内的首位则取反义,[^5]表示匹配除了5以外的任何字符 |
$ | 匹配字符串的结束,如果置于[]内则消除了其特殊含义。[akm$]将匹配a,k,m ,或者 $ |
\s* | 表示连续空格的字符串 |
[ ] | 选择方括号中任意一个,[0-2]等价于[012], [Rr] 负责匹配字母R和r |
{} | 前面的字符或表达式的重复次数。如{5,12} 表示重复次数不能小于5,不能多于12,否则都不匹配 |
{n,} | 重复n次或更多次 |
* | 匹配零个或任意多个字符或字符集合 |
+ | 匹配一个或多个字符,至少匹配一次 |
? | 匹配零个或一个字符 |
. | 匹配除换行符以外的任意字符 |
() | 表示一个字符组,括号内的字符串将作为一个整体被匹配 |
Ⅳ.举例
① 查找单词
text <- c("Don't", "aim", "for", "success", "if", "you", "want", "it", "just","do",
"what","you", "And","believe","in","and","it","will","come","naturally")
# 查找含有 an组合 的单词
grep("[Aa]n", text)
grep("[Aa]n", text, value = TRUE)
Output:
text <- c("Don't", "aim", "for", "success", "if", "you", "want", "it", "just","do",
+ "what","you", "And","believe","in","and","it","will","come","naturally")
>
> # 查找含有 an组合 的单词
> grep("[Aa]n", text)
[1] 7 13 16
> # 查找含有 an组合 的单词
> grep("[Aa]n", text, value = TRUE)
[1] "want" "And" "and"
② 邮箱匹配
text2 <- c("My educational email address is 22013062@zju.edu.cn.",
"My QQ email address is 2568742957@qq.com.", "I have a dream,
I want to go to the university of Tokyo for Phd degree.")
# email address match
grep("[0-9]+@[a-z]+(.[a-z]+)+", text2, value = TRUE)
regexpr("[0-9]+@[a-z]+(.[a-z]+)+", text2)
grel("[0-9]+@[a-z]+(.[a-z]+)+", text2)
m = regexpr("[0-9]+@[a-z]+(.[a-z]+)+", text2)
regmatches(text2, m)
library(stringr)
str_extract(text2,"[0-9]+@[a-z]+(.[a-z]+)+")
str_extract_all(text2,"[0-9]+@[a-z]+(.[a-z]+)+")
Output:
> text2 <- c("My educational email address is 22013062@zju.edu.cn.",
+ "My QQ email address is 2568742957@qq.com.", "I have a dream,
+ I want to go to the university of Tokyo for Phd degree.")
> # email address match
> grep("[0-9]+@[a-z]+(.[a-z]+)+", text2, value = TRUE)
[1] "My educational email address is 22013062@zju.edu.cn."
"My QQ email address is 2568742957@qq.com."
> regexpr("[0-9]+@[a-z]+(.[a-z]+)+", text2)
[1] 33 24 -1
attr(,"match.length")
[1] 19 17 -1
attr(,"index.type")
[1] "chars"
attr(,"useBytes")
[1] TRUE
> grepl("[0-9]+@[a-z]+(.[a-z]+)+", text2)
[1] TRUE TRUE FALSE
> m = regexpr("[0-9]+@[a-z]+(.[a-z]+)+", text2)
> regmatches(text2, m)
[1] "22013062@zju.edu.cn" "2568742957@qq.com"
> library(stringr)
> str_extract(text2,"[0-9]+@[a-z]+(.[a-z]+)+")
[1] "22013062@zju.edu.cn" "2568742957@qq.com" NA
> str_extract_all(text2,"[0-9]+@[a-z]+(.[a-z]+)+")
[[1]]
[1] "22013062@zju.edu.cn"
[[2]]
[1] "2568742957@qq.com"
[[3]]
character(0)