2013-02-09 230 views
6

如何提取指定字符的所有字符?舉例來說,我想提取「。」之前的所有內容。 (期):從字符串中提取字符

a<-c("asdasd.sss","segssddfge.sss","se.sss") 

我想回去:

asdasd segssddfge se 

我想:

substr(a,1,".") 

,但它似乎並沒有工作。

有什麼想法?

+0

它是一個CSV文件,所以應該只有一個「」 – user1234440 2013-02-09 16:44:07

回答

7

這裏是一個非常基本的方法:

sapply(strsplit(a, "\\."), `[[`, 1) 
# [1] "asdasd"  "segssddfge" "se" 

而另:

sub(".sss", "", a, fixed = TRUE) 
# [1] "asdasd"  "segssddfge" "se" 
## OR sub("(.*)\\..*", "\\1", a) 
## And possibly other variations 
+1

@Arun,忘了加上「'固定= TRUE;」這是我的方法,企圖正在基於對OP數據的假設(也許是錯誤的)。謝謝。 – A5C1D2H2I1M1N2O1R2T1 2013-02-09 17:00:32

4

使用sub

# match a "." (escape with "\" to search for "." as a normal "." 
# means "any character") followed by 0 to any amount of characters 
# until the end of the string and replace with nothing ("") 
sub("\\..*$", "", a) 

使用subtrgregexpr(假設只有1 .有,有一個在所有條紋確定匹配向量中的ngs)。

# get the match position of a "." for every string in "a" (returns a list) 
# unlist it and get the substring of each from 1 to match.position - 1 
substr(a, 1, unlist(gregexpr("\\.", a)) - 1) 
2

這裏使用gsub

gsub(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss")) 
[1] "asdasd"  "segssddfge" "se"