R的grep和grepl

 

grep(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)

 

参数

取值说明

ignore.case

是否区分大小写

perl

是否使用perl规则

fixed

是否精确匹配

value

FALSE返回对应的索引

TRUE返回匹配到的结果

useBytes

FALSE匹配character-by-character

TRUE 匹配byte-by-byte

invert

FALSE返回匹配到的

TRUE返回没有被匹配的

 

 

 

 

> grep("[a-z]", letters)

 [1]  1  2  3  4  5  6  7  8  9 10 11 12

[13] 13 14 15 16 17 18 19 20 21 22 23 24

[25] 25 26

> grep("[a-z]", letters,value = TRUE)

 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i"

[10] "j" "k" "l" "m" "n" "o" "p" "q" "r"

[19] "s" "t" "u" "v" "w" "x" "y" "z"

 

> txt<-c("Spark","RDD","Scala","MLlib","GraghX")

> grep("^S.*",txt,value = TRUE)

[1] "Spark" "Scala"

 

text<-c("ben@sina.com",

        "kate@sina..com",

        "ka...te@sina.com",

        "kate@sina.12",

        "kate@12.sina.com")

grep("\\w+@[[:alpha:]]+\\.[[:alpha:]]+",text,value=TRUE)

posted @ 2015-07-01 20:41  智能先行者  阅读(6730)  评论(0编辑  收藏  举报