簡體   English   中英

R替換所有屬於網站的子字符串

[英]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)

模式來自: 本網站

代碼運行但不起作用。 有任何想法嗎?

去掉:

  • ^$ ,與行的開始/結尾匹配
  • first和last / ,它們是分隔符,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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM