Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用r删除文本文档中没有http的URL_R_Regex_Text Mining - Fatal编程技术网

如何使用r删除文本文档中没有http的URL

如何使用r删除文本文档中没有http的URL,r,regex,text-mining,R,Regex,Text Mining,我正在尝试从一个大文本文件中删除可能以http/https开头或不以http/https开头的url,该文件保存在R中的urldoc中。url可能以tinyurl.com/ydyzzlkk或aclj.us/2y6dQKw或pic.twitter.com/ZH08wej40K开头。基本上,我希望在找到空格后删除“/”之前的数据,在找到空格之前删除“/”之后的数据。我尝试了很多模式,搜索了很多地方。无法完成任务。如果你能提供一些意见,我会帮我很多 这是我最后一次尝试并因上述问题而陷入困境。 urld

我正在尝试从一个大文本文件中删除可能以http/https开头或不以http/https开头的url,该文件保存在R中的urldoc中。url可能以tinyurl.com/ydyzzlkk或aclj.us/2y6dQKw或pic.twitter.com/ZH08wej40K开头。基本上,我希望在找到空格后删除“/”之前的数据,在找到空格之前删除“/”之后的数据。我尝试了很多模式,搜索了很多地方。无法完成任务。如果你能提供一些意见,我会帮我很多

这是我最后一次尝试并因上述问题而陷入困境。 urldoc=gsub(“?[a-z]+\..\/.[s]$”,“”,urldoc)

投入将是:他的职业的耻辱。pic.twitter.com/ZH08wej40K在宗教自由的重大胜利中,管理员。继续走这条路的机构已经被切除了内脏。goo.gl/YmNELW一点也不像管理员。提案:tinyurl.com/ydyzzlkk

我期待的结果是:这是对他的职业的耻辱。在宗教自由的一次重大胜利中,行政长官。继续走这条路的机构已经被切除了内脏。一点也不像管理员。提议:


谢谢。

根据您的规格,您可以使用以下正则表达式:

\s*[^ /]+/[^ /]+

详细信息

  • \s*
    -0个或更多空格字符
  • [^/]+
    (或
    [^[:space://]
    )-除空格(或空格)和
    /
    以外的任何一个或多个字符
  • /
    -斜杠
  • [^/]+
    (或
    [^[:space://]
    )-除空格(或空格)和
    /
    以外的任何一个或多个字符
:

如果要考虑任何空格,请将文字空格替换为
[:space://code>

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 ,"

text请参见已回答的问题,但如果您以前没有遇到过
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, "")
#最完整的字符串操作包
图书馆(stringi)
#文本和正则表达式

文本如果您包含一个简单的示例输入和所需的输出,可以用来测试和验证可能的解决方案,则更容易帮助您。显示您尝试的内容并准确说出您遇到的问题。请尝试
urldoc=gsub(“\\s*[^/]+/[^/]+”,“”,urldoc)
,请参阅。感谢您的输入。我添加了输入、输出和我尝试的语句。请查收,谢谢。成功了。但它只留下了一个部分url“https://”。其他一切都被移除了。你能建议任何正则表达式删除它吗?@srk3124
urldoc=gsub(“\\s*(?:https?:/)?[^/]+/[^/]+,”,urldoc)
?或
urldoc=gsub(\\s*(?:[^/]+/[^/]+| https?:/),“”,urldoc)
?谢谢。问题解决了。我是R的初学者,我正在努力追赶。你能给我推荐一本书让我更好地了解R吗?@srk3124我想你应该自己找一些适合你特殊需要的书,我只能推荐你在这里遵循R标签。看,也许它会帮你找到一些东西。
# 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, "")