Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract URL parameters and values in R

Tags:

regex

r

gsub

We want to extract parameters and values from a given URL like

http://www.exemple.com/?a=1&b=2&c=3#def

Using xml2::url_parse we were able to Parse a url into its component pieces. However we still want to devide the query into elements using gsub matching regular expression:

([^?&=#]+)=([^&#]*)  

Desired output

a=1
b=2
c=3
like image 306
SalimK Avatar asked Feb 08 '23 00:02

SalimK


1 Answers

Use urltools package to parse URLs.

> u <- "http://www.exemple.com/?a=1&b=2&c=3#def"
> strsplit(urltools::parameters(u), "&")[[1L]]
[1] "a=1" "b=2" "c=3"
> urltools::param_get(u, "b")
  b
1 2
like image 115
Artem Klevtsov Avatar answered Feb 16 '23 02:02

Artem Klevtsov