在 URL "/" 中的特定字符串之间提取文本
Extract text between specific string in a URL "/"
我正在尝试收集特定字符集之前的所有内容
即我有一个 URL 如下
url = "https://www.somewebsiteLink.com/someDirectory/Directory/ascensor/163235494/d"
url2 = "https://www.somewebsiteLink.com/someDirectory/Directory/aire-acondicionado-calefaccion-ascensor/45837493/d
我想从链接中提取两件事:
Link 1: ascensor
和 163235494
Link 2: aire-acondicionado-calefaccion-ascensor
和 45837493
因此,最后一个 /
之间的数字以及最后一个 2 /
之间的文本 /
.
拆分 /
上的字符串并将第 3 个和第 2 个元素拉到最后一个元素:
url = "https://www.somewebsiteLink.com/someDirectory/Directory/ascensor/163235494/d"
url2 = "https://www.somewebsiteLink.com/someDirectory/Directory/aire-acondicionado-calefaccion-ascensor/45837493/d"
urls = c(url, url2)
pieces = strsplit(urls, split = "/")
result = lapply(pieces, \(x) x[length(x) - 2:1])
## for older R verions:
# result = lapply(pieces, function(x) x[length(x) - 2:1])
result
# [[1]]
# [1] "ascensor" "163235494"
#
# [[2]]
# [1] "aire-acondicionado-calefaccion-ascensor" "45837493"
我正在尝试收集特定字符集之前的所有内容
即我有一个 URL 如下
url = "https://www.somewebsiteLink.com/someDirectory/Directory/ascensor/163235494/d"
url2 = "https://www.somewebsiteLink.com/someDirectory/Directory/aire-acondicionado-calefaccion-ascensor/45837493/d
我想从链接中提取两件事:
Link 1: ascensor
和 163235494
Link 2: aire-acondicionado-calefaccion-ascensor
和 45837493
因此,最后一个 /
之间的数字以及最后一个 2 /
之间的文本 /
.
拆分 /
上的字符串并将第 3 个和第 2 个元素拉到最后一个元素:
url = "https://www.somewebsiteLink.com/someDirectory/Directory/ascensor/163235494/d"
url2 = "https://www.somewebsiteLink.com/someDirectory/Directory/aire-acondicionado-calefaccion-ascensor/45837493/d"
urls = c(url, url2)
pieces = strsplit(urls, split = "/")
result = lapply(pieces, \(x) x[length(x) - 2:1])
## for older R verions:
# result = lapply(pieces, function(x) x[length(x) - 2:1])
result
# [[1]]
# [1] "ascensor" "163235494"
#
# [[2]]
# [1] "aire-acondicionado-calefaccion-ascensor" "45837493"