[英]R replace all substrings that are websites
我努力了
gsub("/^(http?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$/","","This is a website http://www.example.com/test and needs to be removed",ignore.case=T, perl=T)
模式來自: 本網站
代碼運行但不起作用。 有任何想法嗎?
去掉:
^
和$
,與行的開始/結尾匹配 /
,它們是分隔符,gsub不需要
,這避免了您僅匹配url-當前,它捕獲了該行的所有結尾) gsub("(http?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w\\.-]*)*\\/?","","This is a website http://www.example.com/test and needs to be removed",ignore.case=T, perl=T)
rm_url
,維護了qdapRegex軟件包中的rm_url
函數。 它具有糾正遺留的多余空白的附加好處:
library(qdapRegex)
rm_url("This is a website http://www.example.com/test and needs to be removed")
## [1] "This is a website and needs to be removed"
如果你有興趣在正則表達式是什么rm_url
可以使用grab
使用單一的正則表達式上的任何qdapRegex功能功能,了解所用的表達:
grab("rm_url")
## [1] "(http[^ ]*)|(ftp[^ ]*)|(www\\.[^ ]*)"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.