【问题标题】:how to remove only "www" from URL not any other words that contain "w" character如何从 URL 中仅删除 \"www\" 而不是包含 \"w\" 字符的任何其他单词
【发布时间】:2022-09-23 09:41:57
【问题描述】:

如何删除 URL(包含 \"www\")但不删除包含 \"w\" 的任何其他单词?

这是我的 R 代码

textz <- \"Please don\'t w8 notification from Www.example.com, just call the office during weekdays\"

# URL without https
text <- gsub(\"(W|w|W|w)(.)(\\\\S*)\", \"\", textz) 
text

# output
[1] \"Please don\'t  notification from  just call the office during \"

如何维护单词“w8”和“weekdays”?我只想在这种情况下删除 URL。先感谢您!

    标签: r regex


    【解决方案1】:

    也许

    textz <- "Please don't w8 notification from Www.example.com, just call the office during weekdays"
    
    # URL without https
    text <- gsub("[wW]{3}\\S+", "", textz) 
    text
    
    #"Please don't w8 notification from  just call the office during weekdays"
    

    这个正则表达式“[wW]{3}\S+”的意思是:
    [wW] 寻找 w 或 W,
    {3} 正好是前一个字符的 3 个。
    \S+ 一个或多个非空格。

    【讨论】:

    • 我发现regexr.com 通常也可以极大地帮助编写正则表达式。
    【解决方案2】:

    也许将字符存储在向量中,然后仅访问向量中前三个项目之后的项目,因为前三个将始终是 www。

    以下是将字符串拆分为单个字符以存储在向量中的方法。

    Determine all characters present in a vector of strings

    当然,如果您必须拆分字符串,以便网站 URL 与其他 URL 分开

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      • 2011-06-13
      • 2014-05-20
      • 2020-02-14
      相关资源
      最近更新 更多