R - Only select last part of a string after the last point

Refresh

April 2019

Views

9 time

0

I have a dataframe with one column that represents the requests made by my users. A few examples look like this:

GET /enviro/html/tris/tris_overview.html
GET /./enviro/gif/emcilogo.gif
GET /docs/exposure/meta_exp.txt.html
GET /hrmd/
GET /icons/circle_logo_small.gif

I only want to select the last part of the string after the last "." in such a way that I return the pagetype of the string. The output of these lines should therefore be:

.html
.gif
.html

.gif

I tried doing this with sub but I only manage to select everything after the first "." example:

tring <- c("GET /enviro/html/tris/tris_overview.html", "GET /./enviro/gif/emcilogo.gif", "GET /docs/exposure/meta_exp.txt.html", "GET /hrmd/", "GET /icons/circle_logo_small.gif")


sub("^[^.]*", "", sapply(strsplit(tring, "\\s+"), `[`, 2))

this returns:

".html"                     
"./enviro/gif/emcilogo.gif" 
".txt.html"                 
""                          
".gif"  

I created the following gsub code that works for string containing two points:

gsub(pattern = ".*\\.", replacement = "", "GET /./enviro/gif/finds.gif", "\\s+")

this returns:

"gif"

However, I cant seem to come up with one gsub/sub that works for all possible input. It should read the string from right to left. Stop when it sees the first "." and return everything that was found after that "."

I am new to R and I can't come up with something that is doing this. Any help would be highly appreciated!

0 answers