Skip to content

Instantly share code, notes, and snippets.

@gomesfellipe
Last active May 7, 2019 14:39
Show Gist options
  • Save gomesfellipe/e9780ec5bb0c51bb67e5e05cec97c71f to your computer and use it in GitHub Desktop.
Save gomesfellipe/e9780ec5bb0c51bb67e5e05cec97c71f to your computer and use it in GitHub Desktop.
Exemplo simples de como detectar e extrair determinada string utilizando o pacote `stringr` (Jeito nutella)
library(stringr)
# Neste exemplo vamos obter tudo que venha apos
# a string "&token=":
# Link de exemplo:
link <- "http://www.google.com/aclk?sa=l&ai=DChcSEwi2vazh_ujhAhWHDJEKHcJaBWYYABAAGgJjZQ&token=AOD64_2roKLol2MibXGLfO6uavtAm10Hhw&ved=0ahUKEwiN26fh_ujhAhV7GLkGHUZSD6cQ0QwIFA&adurl="
# Primeiro vamos localizar a posicao
# inicial da string desejada
posicao_inicial <- str_locate(link ,"&token=")[,"start"]
# Obter tudo que venha apos a posicao detectada:
link %>% str_sub(posicao_inicial) %>% str_remove("&token=")
# TODO: Transformar em uma funcao
# Fazer o mesmo usando regex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment