如何使用 r 删除文本文档中没有 http 的 url
How to remove urls without http in a text document using r
我正在尝试从一个大型文本文件中删除可能以 http/https 开头或不以 http/https 开头的 urls,我将其保存在 urldoc in R 中。url 可能以 tinyurl.com/ydyzzlkk 或 aclj.us/2y6dQKw 或 pic.twitter.com/ZH08wej40K 开头。基本上,我想在找到 space 之后删除“/”之前的数据,并在找到 space 之前删除“/”之后的数据。我尝试了很多模式并搜索了很多地方。无法完成任务。如果你能提供一些意见,我会帮助我很多。
这是我尝试过的最后一条语句,但由于上述问题而卡住了。
urldoc = gsub("?[a-z]+\..\/.[\s]$","", urldoc)
输入将是:他职业的耻辱。 pic.twitter.com/ZH08wej40K 宗教自由的重大胜利,Admin。已经剔除机构继续这条道路。 goo.gl/YmNELW 一点也不像管理员。建议:tinyurl.com/ydyzzlkk
我期望的输出是:他职业的耻辱。在宗教自由的重大胜利中,Admin。已经剔除机构继续这条道路。没有什么像管理员。提议:
谢谢。
根据您的规格,您可以使用以下正则表达式:
\s*[^ /]+/[^ /]+
参见regex demo。
详情
\s*
- 0 个或更多白色space 个字符
[^ /]+
(或[^[:space:]/]
)-除space(或白色space)和/
[=41=以外的任何1个或多个字符]
/
- 斜杠
[^ /]+
(或[^[:space:]/]
)- space(或白色space)和/
.[=41 以外的任何 1 个或多个字符=]
urldoc = gsub("\s*[^ /]+/[^ /]+","", urldoc)
如果您想考虑任何白色space,请将文字 space 替换为 [:space:]
、
urldoc = gsub("\s*[^[:space:]/]+/[^[:space:]/]+","", urldoc)
这可能有效:
text <- " http:/thisisanurl.wde , thisaint , nope , uihfs/yay"
words <- strsplit(text, " ")[[1]]
isurl <- sapply(words, function(x) grepl("/",x))
result <- paste0(words[!isurl], collapse = " ")
result
[1] " , thisaint , nope ,"
看到已经回答了,但是如果你以前没有遇到过stringi
,这里有一个替代方案
# most complete package for string manipulation
library(stringi)
# text and regex
text <- "A disgrace to his profession. pic.twitter.com/ZH08wej40K In a major victory for religious liberty, the Admin. has eviscerated institution continuing this path. goo.gl/YmNELW nothing like the admin. proposal: tinyurl.com/ydyzzlkk"
pattern <- "(?:\s)[^\s\.]*\.[^\s]+"
# see what is captured
stringi::stri_extract_all_regex(text, pattern)
# remove (replace with "")
stringi::stri_replace_all_regex(text, pattern, "")
我正在尝试从一个大型文本文件中删除可能以 http/https 开头或不以 http/https 开头的 urls,我将其保存在 urldoc in R 中。url 可能以 tinyurl.com/ydyzzlkk 或 aclj.us/2y6dQKw 或 pic.twitter.com/ZH08wej40K 开头。基本上,我想在找到 space 之后删除“/”之前的数据,并在找到 space 之前删除“/”之后的数据。我尝试了很多模式并搜索了很多地方。无法完成任务。如果你能提供一些意见,我会帮助我很多。
这是我尝试过的最后一条语句,但由于上述问题而卡住了。 urldoc = gsub("?[a-z]+\..\/.[\s]$","", urldoc)
输入将是:他职业的耻辱。 pic.twitter.com/ZH08wej40K 宗教自由的重大胜利,Admin。已经剔除机构继续这条道路。 goo.gl/YmNELW 一点也不像管理员。建议:tinyurl.com/ydyzzlkk
我期望的输出是:他职业的耻辱。在宗教自由的重大胜利中,Admin。已经剔除机构继续这条道路。没有什么像管理员。提议:
谢谢。
根据您的规格,您可以使用以下正则表达式:
\s*[^ /]+/[^ /]+
参见regex demo。
详情
\s*
- 0 个或更多白色space 个字符[^ /]+
(或[^[:space:]/]
)-除space(或白色space)和/
[=41=以外的任何1个或多个字符]/
- 斜杠[^ /]+
(或[^[:space:]/]
)- space(或白色space)和/
.[=41 以外的任何 1 个或多个字符=]
urldoc = gsub("\s*[^ /]+/[^ /]+","", urldoc)
如果您想考虑任何白色space,请将文字 space 替换为 [:space:]
、
urldoc = gsub("\s*[^[:space:]/]+/[^[:space:]/]+","", urldoc)
这可能有效:
text <- " http:/thisisanurl.wde , thisaint , nope , uihfs/yay"
words <- strsplit(text, " ")[[1]]
isurl <- sapply(words, function(x) grepl("/",x))
result <- paste0(words[!isurl], collapse = " ")
result
[1] " , thisaint , nope ,"
看到已经回答了,但是如果你以前没有遇到过stringi
,这里有一个替代方案
# most complete package for string manipulation
library(stringi)
# text and regex
text <- "A disgrace to his profession. pic.twitter.com/ZH08wej40K In a major victory for religious liberty, the Admin. has eviscerated institution continuing this path. goo.gl/YmNELW nothing like the admin. proposal: tinyurl.com/ydyzzlkk"
pattern <- "(?:\s)[^\s\.]*\.[^\s]+"
# see what is captured
stringi::stri_extract_all_regex(text, pattern)
# remove (replace with "")
stringi::stri_replace_all_regex(text, pattern, "")