【问题标题】:Stringr str_replace_all misses repeated termsStringr str_replace_all 遗漏重复项
【发布时间】:2021-05-17 19:12:04
【问题描述】:

我遇到了 stringr::str_replace_all 函数的问题。我正在尝试用 insuredvehicle 替换所有 iv 实例,但该函数似乎只捕获第一个术语。

temp_data <- data.table(text = 'the driver of the 1st vehicle hit the iv iv at a stop')
temp_data[, new_text := stringr::str_replace_all(pattern = ' iv ', replacement = ' insuredvehicle ', string = text)]

结果如下所示,错过了第二个iv项:

1:第一辆车的司机在停车处撞上了被保险车辆iv

我认为问题在于 2 个实例共享一个空间,这是搜索模式的一部分。我这样做是因为我想替换 iv 术语,而不是 driver 中的 iv

我不想简单地将重复的术语合并为 1。我希望结果如下所示:

1:第一辆车的司机在停车处撞上了被保险车辆

如果能帮我解决这个问题,我将不胜感激!

【问题讨论】:

    标签: r regex str-replace stringr


    【解决方案1】:

    也许如果你在你的正则表达式中包含一个单词边界,而不是从替换中删除空格?当您只想要一个与模式匹配的完整单词而不是单词的一部分时,它是理想的,同时远离这些空格问题。 \\b似乎可以解决问题

    temp_data[, new_text := stringr::str_replace_all(pattern = '\\biv\\b', replacement = 'insuredvehicle', string = text)]
    
    new_text
    
    1: the driver of the 1st vehicle hit the insuredvehicle insuredvehicle at a stop
    

    【讨论】:

    • 这非常简单,并且可以与我正在运行的其他代码无缝协作。似乎比在模式周围有空格更好的解决方案。使其阅读起来有点困难,但可以完成工作!谢谢!
    【解决方案2】:

    您可以使用环视:

    temp_data[, new_text := stringr::str_replace_all(pattern = '(?<= )iv(?= )', replacement = 'insuredvehicle', string = text)]
    

    输出:

    "the driver of the 1st vehicle hit the insuredvehicle insuredvehicle at a stop"
    

    【讨论】:

      【解决方案3】:

      使用gsub:

      gsub("\\biv\\b", "insuredvehicle", temp_data$text)
      [1] "the driver of the 1st vehicle hit the uninsuredvehicle uninsuredvehicle at a stop"
      

      【讨论】:

        【解决方案4】:

        使用空间边界:

        temp_data <- data.table(text = 'the driver of the 1st vehicle hit the iv iv at a stop')
        temp_data[, new_text := stringr::str_replace_all(pattern = '(?<!\\S)iv(?!\\S)', replacement = 'insuredvehicle', string = text)]
        

        regex proof

        解释

        --------------------------------------------------------------------------------
          (?<!                     look behind to see if there is not:
        --------------------------------------------------------------------------------
            \S                       non-whitespace (all but \n, \r, \t, \f,
                                     and " ")
        --------------------------------------------------------------------------------
          )                        end of look-behind
        --------------------------------------------------------------------------------
          iv                       'iv'
        --------------------------------------------------------------------------------
          (?!                      look ahead to see if there is not:
        --------------------------------------------------------------------------------
            \S                       non-whitespace (all but \n, \r, \t, \f,
                                     and " ")
        --------------------------------------------------------------------------------
          )                        end of look-ahead
        

        【讨论】:

        • 非常感谢您的解释!这对于了解正则表达式发生了什么非常有帮助。我喜欢这个解决方案,因为它使搜索模式相当容易看到。
        猜你喜欢
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多