首页 > 解决方案 > 清理数据/从数据中截断短 URL

问题描述

我目前正在清理来自 eCom 的一些 URL 数据,因为我想更好地了解引荐来源网址流量的来源。

我在 R 中尝试过 sub() 函数,但在正确应用 RegEx 时遇到了困难。

sub("*.com", "", q2$Session.First.Referrer)

我想简单地清理一个看起来像“http\://www\.gazelle\.com/main/home\.jhtml”的 URL,基本 URL 就是“www.gazelle.com”。

标签: rregexurltexttruncate

解决方案


str_extractstringr包中使用(tidyverse 的一部分):

library(tidyverse)
library(stringr)

my_data <- tibble(addresses = c("https://www.fivethirtyeight.com/features/is-there-still-room-in-the-democratic-primary-for-biden/",
                                "https://www.docs.aws.amazon.com/sagemaker/latest/dg/sms.html",
                                "https://www.stackoverflow.com/questions/55500553/cleaning-data-truncate-short-url-out-of-data"))

str_extract(my_data$addresses, "www.+com")

返回:

[1] "www.fivethirtyeight.com" "www.docs.aws.amazon.com"
[3] "www.stackoverflow.com"  

推荐阅读