这是我想要在select查询和过滤查询中提取字符串的网址:
http://services.odata.org/V4/(S(cscsmmmc110sj01dvwgyolkm))/TripPinServiceRW/People?$ select = ID,CLASS,FIRST_NAME,LAST_NAME& $ filter = FirstName eq'Angel'或FirstName eq'Clyde'
我想在2个字符串中提取select和filter查询值。我试过这些:
res <- str_match(a, "STR1 (.*?) STR2")
我尝试使用字符串处理提取值,如相关问题(Extract string between /)所示,但我无法提取值。还有其他方法可以提供帮助吗?
答案 0 :(得分:4)
httr
有一个parse_url
函数:
library(httr)
myurl <- "http://services.odata.org/V4/(S(cscsmmmc110sj01dvwgyolkm))/TripPinServiceRW/People?$select=ID,CLASS,FIRST_NAME,LAST_NAME&$filter=FirstName eq 'Angel' or FirstName eq 'Clyde'"
parse_url(myurl)
输出:
$scheme
[1] "http"
$hostname
[1] "services.odata.org"
$port
NULL
$path
[1] "V4/(S(cscsmmmc110sj01dvwgyolkm))/TripPinServiceRW/People"
$query
$query$`$select`
[1] "ID,CLASS,FIRST_NAME,LAST_NAME"
$query$`$filter`
[1] "FirstName eq 'Angel' or FirstName eq 'Clyde'"
$params
NULL
$fragment
NULL
$username
NULL
$password
NULL
attr(,"class")
[1] "url"
答案 1 :(得分:3)
通过$
分割网址,然后提取select
和filter
部分来解决问题。
foo <- "http://services.odata.org/V4/(S(cscsmmmc110sj01dvwgyolkm))/TripPinServiceRW/People?$select=ID,CLASS,FIRST_NAME,LAST_NAME&$filter=FirstName eq 'Angel' or FirstName eq 'Clyde'"
bar <- unlist(strsplit(foo, "\\$"))
bar
[1] "http://services.odata.org/V4/(S(cscsmmmc110sj01dvwgyolkm))/TripPinServiceRW/People?"
[2] "select=ID,CLASS,FIRST_NAME,LAST_NAME&"
[3] "filter=FirstName eq 'Angel' or FirstName eq 'Clyde'"
resultSelect <- unlist(strsplit(gsub("select=|\\&$", "", grep("^select=", bar, value = TRUE)), ","))
resultFilter <- unlist(strsplit(gsub("filter=", "", grep("^filter=", bar, value = TRUE)), ","))
resultSelect
[1] "ID" "CLASS" "FIRST_NAME" "LAST_NAME"
resultFilter
[1] "FirstName eq 'Angel' or FirstName eq 'Clyde'"
使用管道和自定义功能的Tidier 示例:
URL <- "http://services.odata.org/V4/(S(cscsmmmc110sj01dvwgyolkm))/TripPinServiceRW/People?$select=ID,CLASS,FIRST_NAME,LAST_NAME&$filter=FirstName eq 'Angel' or FirstName eq 'Clyde'"
queries <- c("select", "filter")
extractQ <- function(x, url = URL) {
library(magrittr)
strsplit(url, "\\$") %>%
unlist() %>%
grep(paste0("^", x, "="), ., value = TRUE) %>%
gsub(paste0(x, "=|\\&$"), "", .) %>%
strsplit(",") %>%
unlist()
}
sapply(queries, extractQ)
$select
[1] "ID" "CLASS" "FIRST_NAME" "LAST_NAME"
$filter
[1] "FirstName eq 'Angel' or FirstName eq 'Clyde'"