简体   繁体   English

提取特殊字符“/”之间的倒数第二个字

[英]extracting the second last word between the special characters “/”

I would like to extract the second last string after the '/' symbol. 我想在'/'符号后面提取第二个最后一个字符串。 For example, 例如,

url<- c('https://example.com/names/ani/digitalcod-org','https://example.com/names/bmc/ambulancecod.org' )
df<- data.frame (url)

I want to extract the second word from the last between the two // and would like to get the words 'ani' and 'bmc' 我想从两个中间的第二个单词中提取第二个单词//并希望得到“ani”和“bmc”这两个词

so, I tried this 所以,我试过这个

 library(stringr)
 df$name<- word(df$url,-2)

I need output which as follows: 我需要输出如下:

name 
ani
bmc 

You can use word but you need to specify the separator, 您可以使用word但需要指定分隔符,

library(stringr)

word(url, -2, sep = '/')
#[1] "ani" "bmc"

Try this: 试试这个:

as.data.frame(sapply(str_extract_all(df$url,"\\w{2,}(?=\\/)"),"["))[3,]
#   V1  V2
#3 ani bmc
  as.data.frame(sapply(str_extract_all(df$url,"\\w{2,}(?=\\/)"),"["))[2:3,]
#   V1    V2
#2 names names
#3   ani   bmc

A non-regex approach using basename 使用basename非正则表达式方法

basename(mapply(sub, pattern = basename(url), replacement = "", x = url, fixed = TRUE))
#[1] "ani" "bmc"

basename(url) "removes all of the path up to and including the last path separator (if any)" and returns basename(url) “删除所有路径,包括最后一个路径分隔符(如果有)”并返回

[1] "digitalcod-org"   "ambulancecod.org"

use mapply to replace this outcome for every element in url by "" and call basename again. 使用mapply""替换url每个元素的结果,并再次调用basename

Use gsub with 使用gsub

.*?([^/]+)/[^/]+$


In R : R

 urls <- c('https://example.com/names/ani/digitalcod-org','https://example.com/names/bmc/ambulancecod.org' ) gsub(".*?([^/]+)/[^/]+$", "\\\\1", urls) 

This yields 这产生了

 [1] "ani" "bmc" 

See a demo on regex101.com . 请参阅regex101.com上的演示

Here is a solution using strsplit 这是使用strsplit的解决方案

words <- strsplit(url, '/')
L <- lengths(words)
vapply(seq_along(words), function (k) words[[k]][L[k]-1], character(1))    
# [1] "ani" "bmc"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM