r - 如何删除字符串中包含“.com”但有链接的所有内容?
问题描述
如何在文本中获得预期的输出,如下例所示?
x<-c("Commerce recommend erkanexample.com.tr. This site erkanexample.com. erkandeneme.com is widely. The company name is apple.commerce is coma. spread")
x<-gsub("(.com)\\S+", "",x)
x
[1] "Commerce r erkanexample This site erkanexample erkandeneme.com is widely. The name is apple is"
expected
[1] "Commerce recommend This site. is widely. The company name is apple.commerce is coma. spread"
>
解决方案
stringr
包提供基本字符串操作的功能:
library(stringr)
library(dplyr)
x %>%
str_split(" ") %>%
unlist() %>%
str_subset("\\.com($|\\.)",negate = TRUE) %>%
str_c(collapse = " ")
给出:
"Commerce recommend This site is widely. The company name is apple.commerce is coma. spread"
编辑后
x %>%
str_split(" ") %>%
unlist() %>%
str_subset("\\.com$", negate = TRUE) %>%
str_replace(".*\\.com.*\\.$", ".") %>%
str_c(collapse = " ") %>%
str_replace_all(" \\.", "\\.")
给出:
"Commerce recommend. This site. is widely. The company name is apple.commerce is coma. spread"
推荐阅读
- php - 如何在php中从浏览器获取url?
- python-3.x - 如何在 mqtt 中同时阅读和发布到两个不同的主题
- python - 在python中创建位图
- php - 大写数组键和小写数组值(来自 parse_str 的输入)
- ruby-on-rails - 弃用警告中的 Ruby 版本错误
- python - Python while 循环 - 类似代码中的差异
- c# - C# 将文件保存到内存并获取在以文件路径为输入的方法中使用的路径
- python - 如何在所有列组合的两个指标之间进行 numpy 计算?
- flutter - 聚焦小部件后键盘自动出现和消失
- python-3.x - 如何将给定的整数视为二进制值?