【问题标题】:In regex, how do you remove two or more consecutive characters using stringr in R?在正则表达式中,如何使用 R 中的 stringr 删除两个或多个连续字符?
【发布时间】:2023-02-07 03:22:09
【问题描述】:

我试图在我的脚本有 2 个或更多个连续滴答时删除任何时间。我从here 了解到,使用正则表达式可以检测到任何带有 (.)\1 的重复字符,因此我将其修改为 (`)\1,但这不起作用。为什么不?

library(stringr)
example <- c("``", "````", "`")
str_replace_all(example, "(`)\1", "gone") #want the first 2 to say 'gone' and the 3rd to stay the same

【问题讨论】:

    标签: r regex stringr


    【解决方案1】:
    library(stringr)
    example <- c("``", "````", "`")
    
    # THIS removes consecutives in pairs
    str_replace_all(example, "(``)+", "gone") 
    
    # THIS removes two or more consecutive
    str_replace_all(example, "``+", "gone") 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      相关资源
      最近更新 更多