I want to split the below string in R
https://bugzilla.mozilla.org/show_bug.cgi?id=797998
Here I want to split the URL and only want to get the value as 797998.<
> library('httr')
> parse_url('https://bugzilla.mozilla.org/show_bug.cgi?id=797998')
$scheme
[1] "https"
$hostname
[1] "bugzilla.mozilla.org"
$port
NULL
$path
[1] "show_bug.cgi"
$query
$query$id
[1] "797998"
$params
NULL
$fragment
NULL
$username
NULL
$password
NULL
attr(,"class")
[1] "url"
I'd recommend the url suggestion above, however if it's stored as a string then a couple of options are:
str <- "https://bugzilla.mozilla.org/show_bug.cgi?id=797998"
# If you know it will follow the only '=' in the string
(num <- unlist(strsplit(str, "="))[2])
# If you know that the number is always the last 6 digits
(num <- substr(str, nchar(str)-5, nchar(str)))
# If you know the number always follows the last '=' sign
revstr <- rev(unlist(strsplit(str, NULL)))
index <- which(revstr == "=")
revnum <- revstr[1:(index-1)]
(num <- paste(rev(revnum), collapse = ""))
Note, you'd have to convert these to numeric, using as.numeric()
, if you wanted a number. Otherwise, they are currently given as character strings.