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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM