2

This is the url from which I want to extract the string in select query and in filter query:

http://services.odata.org/V4/(S(cscsmmmc110sj01dvwgyolkm))/TripPinServiceRW/People?$select=ID,CLASS,FIRST_NAME,LAST_NAME&$filter=FirstName eq 'Angel' or FirstName eq 'Clyde'

I would like to extract select and filter query values in 2 strings. I have tried these:

res <- str_match(a, "STR1 (.*?) STR2")

I have tried extracting values using string handling, as shown in this related question (Extract string between /), but I was not able to extract the values. Is there any other method which can help?

2 Answers 2

6

httr has a parse_url function:

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)

Output:

$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"
Sign up to request clarification or add additional context in comments.

Comments

3

Solution by splitting url by $ and then extracting select and filter parts.

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 example using pipes and custom function:

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'"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.