Open rzldasb opened 5 years ago
grep: 返回vector grepl: 返回逻辑值
files <- list.files("c:/windows")
grep("\\.exe$", files) 以.exe结尾的文件名的vector值
[1] 8 28 30 35 36 57 68 98 99 101 110 111 114 116
grepl("\\.exe$", files) 逻辑判断是否是以.exe结尾
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
[14] FALSE FALSE FALSE 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" "winhlp32.exe"
[13] "write.exe" "xinstaller.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" "winhlp32.exe"
[13] "write.exe" "xinstaller.exe"
> text
[1] "Hello Adam!\nHello Ava!"
> sub(pattern="Adam", replacement="world", text) ##sub
[1] "Hello world!\nHello Ava!"
> text
[1] "Hello Adam!\nHello Ava!"
> sub(pattern="Adam|Ava", replacement="world", text)
[1] "Hello world!\nHello Ava!"
> gsub(pattern="Adam|Ava", replacement="world", text)
[1] "Hello world!\nHello world!"
substr
> x <- "123456789"
> substr(x, c(2,4), c(4,5,8))
[1] "234"
> substring(x, c(2,4), c(4,5,8))
[1] "234" "45" "2345678"
正则表达式的简介
元字符
[ ] \ ^ $ . | ? * + ( )