2016-08-10 30 views
0

我有一些文本字符串,我想從中提取某些信息位。特別是我想從10中提取評分。R正則表達式提取等級從字符串中抽取10個

我想在構造函數func_to_extract_rating,做以下的幫助...

text_string_vec <- c('blah$2.94 blah blah 3/10 blah blah.', 
        'foo foo 8/10.', 
        '10/10 bar bar21/09/2010 bar bar', 
        'jdsfs1/10djflks5/10.') 

func_to_extract_rating <- function(){} 

output <- lapply(text_string_vec,func_to_extract_rating) 
output 
[[1]] 
[1] 3 10 

[[2]] 
[1] 8 10 

[[3]] 
[1] 10 10 

[[4]] 
[[4]][[1]] 
[1] 1 10 

[[4]][[2]] 
[1] 5 10 
+0

'y < - regmatches(text_string_vec,gregexpr('\\ d +/10',text_string_vec)); rapply(y,strsplit,split ='/',how ='list')' – rawr

回答

3

事情是這樣的,也許:

library(stringr) 

result = str_extract_all(text_string_vec, "[0-9]{1,2}/10") 
result = lapply(result, function(x) gsub("/"," ", x)) 

[[1]] 
[1] "3 10" 

[[2]] 
[1] "8 10" 

[[3]] 
[1] "10 10" 

[[4]] 
[1] "1 10" "5 10" 

但是,因爲它總是出10,如果你只是想要數字評分,你可以這樣做:

result = str_extract_all(text_string_vec, "[0-9]{1,2}/10") 
result = lapply(result, function(x) as.numeric(gsub("/10","", x))) 
1

這是base R選項

lapply(strsplit(str1, "([0-9]{1,2}\\/10)(*SKIP)(*FAIL)|.", perl = TRUE), 
     function(x) { 
     lst <- lapply(strsplit(x[nzchar(x)], "/"), as.numeric) 
     if(length(lst)==1) unlist(lst) else lst}) 
#[[1]] 
#[1] 3 10 

#[[2]] 
#[1] 8 10 

#[[3]] 
#[1] 10 10 

#[[4]] 
#[[4]][[1]] 
#[1] 1 10 

#[[4]][[2]] 
#[1] 5 10