How to split a url

前端 未结 2 1802
北荒
北荒 2021-01-27 05:12

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.<

2条回答
  •  佛祖请我去吃肉
    2021-01-27 05:48

    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.

提交回复
热议问题