How to extract a specific part of a string

Question:

Let me have this URL extracted

/ac/rio-branco/xpto-xyz-1-0-16-5-abcd-a1G57000003DE4QEAW

And I only want the snippet that starts with a1G , does anyone know how I can get only this snippet?

Answer:

You can do it using stringr package and regular expressions.

In your case, I would do it like this:

s <- "/ac/rio-branco/xpto-xyz-1-0-16-5-abcd-a1G57000003DE4QEAW"
stringr::str_extract(s, "a1G\\S+\\s*")
[1] "a1G57000003DE4QEAW"

This code works even if s is a vector, so it would work on a data.frame like this:

df$extrair <- stringr::str_extract(df$url, "a1G\\S+\\s*")

Note that if you do not have the stringr package installed, you will need to install it using the install.packages("stringr") command.

Scroll to Top