[英]R/Stringr Extract String after nth occurrence of "_" and end with first occurrence of "_"
[英]R - Extract info after nth occurrence of a character from the right of string
我已經看過許多提取w / gsub
迭代,但它們主要處理從左到右或一次出現之后的提取。 我想從右到左匹配,計算四次出現-
,匹配第3次和第4次出現之間的所有事情。
例如:
string outcome
here-are-some-words-to-try some
a-b-c-d-e-f-g-h-i f
以下是我嘗試過的一些參考資料:
x = c("here-are-some-words-to-try", "a-b-c-d-e-f-g-h-i")
sapply(x, function(strings){
ind = unlist(gregexpr(pattern = "-", text = strings))
if (length(ind) < 4){NA}
else{substr(strings, ind[length(ind) - 3] + 1, ind[length(ind) - 2] - 1)}
})
#here-are-some-words-to-try a-b-c-d-e-f-g-h-i
# "some" "f"
你可以用
([^-]+)(?:-[^-]+){3}$
請參閱regex101.com上的演示 。
R
這可能是
library(dplyr) library(stringr) df <- data.frame(string = c('here-are-some-words-to-try', 'abcdefgh-i', ' no dash in here'), stringsAsFactors = FALSE) df <- df %>% mutate(outcome = str_match(string, '([^-]+)(?:-[^-]+){3}$')[,2]) df
和收益率
string outcome 1 here-are-some-words-to-try some 2 abcdefghi f 3 no dash in here <NA>
分裂你的句子怎么樣? 就像是
string <- "here-are-some-words-to-try"
# separate all words
val <- strsplit(string, "-")[[1]]
# reverse the order
val rev(val)
# take the 4th element
val[4]
# And using a dataframe
library(tidyverse)
tibble(string = c("here-are-some-words-to-try", "a-b-c-d-e-f-g-h-i")) %>%
mutate(outcome = map_chr(string, function(s) rev(strsplit(s, "-")[[1]])[4]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.