6.從一行文本中刪除標(biāo)點符號
going <- "a1~!@#$%^&*bcd(){}_+:efg\"<>?,./;'[]-="
gsub(pattern = "[[:punct:]]+",replacement = "",x = going)
7.從包含字母數(shù)字字符的字符串中刪除數(shù)字
c2 <- "day of 2nd ID5 Conference 19 12 2005"
從上面的字符串中,所需的輸出是“第二次ID5會議日”。 您不能使用簡單的“[[:digit:]] +”正則表達式,因為它將匹配給定字符串中的所有可用數(shù)字。 相反,在這種情況下,我們將檢測數(shù)字邊界以獲得所需的結(jié)果。
gsub(pattern = "\\b\\d+\\b",replacement = "",x = c2)
8.在字符串中查找數(shù)字的位置
string <- "there were 2 players each in 8 teams"
gregexpr(pattern = '\\d',text = string) #or
unlist(gregexpr(pattern = '\\d',text = "there were 2 players each in 8 teams"))
9.在字符串中提取括號內(nèi)的可用信息(括號)
string <- "What are we doing tomorrow ? (laugh) Play soccer (groans) (cries)"
gsub("[\\(\\)]","",regmatches(string, gregexpr("\\(.*?\\)", string))[[1]])
說明:在此解決方案中,我們使用了惰性匹配技術(shù)。 首先,使用regmatches,我們用諸如(cries)(呻吟)之類的詞來提取括號(笑)。 然后,我們只需使用gsub()函數(shù)刪除括號。
10.僅提取范圍中的第一個數(shù)字
x <- c("75 to 79", "80 to 84", "85 to 89")
gsub(" .*\\d+", "", x)
11.從給定字符串中提取電子郵件地址
string <- c("My email address is abc@boeing.com","my email address is def@jobs.com","aescher koeif","paul renne")
unlist(regmatches(x = string, gregexpr(pattern = "[[:alnum:]]+\\@[[:alpha:]]+\\.com",text = string)))








暫無數(shù)據(jù)