2017-10-10 45 views
0

我有一個載體一些字符串,如:r用str_extract(stringr)導出一個字符串之間「_」

x <- c("ROH_Pete_NA_1_2017.zip", 
    "ROH_Annette_SA_2_2016.zip", 
    "ROH_Steve_MF_4_2015.zip") 

我需要提取的名字出這個strings (Pete, Annette, Steve) 的,我想這樣做,在一個循環中,並與str_extract()

所有字符串以ROH_開頭,但名稱的長度是不同的,也是字符串後面。

我想用str_extract(),但我也很高興其他的解決方案

謝謝您的幫助。

回答

3

您可能使用str_match更好,因爲這樣可以捕獲組。 因此,您可以將_添加到上下文中,但只返回您感興趣的位。(\\w+?)是捕獲組,並且str_match將此作爲第二列返回,因此[,2](第一列是str_extract將返回的那一列) 。

library(stringr) 
str_match(x,"ROH_(\\w+?)_")[,2] 

[1] "Pete" "Annette" "Steve" 
0

您可以使用base函數sub。

sub("ROH_([[:alpha:]]+)_.*","\\1",x,perl=T) 

[1] "Pete" "Annette" "Steve" 
1

這裏是str_extract的解決方案:

library(stringr) 
str_extract(x, "(?<=_).+?(?=_)") 
# [1] "Pete" "Annette" "Steve" 

您還可以使用gsub在基礎R:

gsub("^.+?_|_.+$", "", x) 
# [1] "Pete" "Annette" "Steve" 
0

嘗試stringi PAC kage:

library(stringi) 
stri_split_fixed(a,"_", simplify = T)[,2] 
[1] "Pete" "Annette" "Steve" 
相關問題