- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 stringr::str_replace_all 函数时遇到问题。我正在尝试将 iv 的所有实例替换为 insuredvehicle,但该函数似乎只包含第一个术语。
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: the driver of the 1st vehicle hit the insuredvehicle iv at a stop
我认为问题在于 2 个实例共享一个空间,这是搜索模式的一部分。我这样做是因为我想替换 iv 术语,而不是 driver 中的 iv。
我不想简单地将重复项合并为 1。我希望结果如下所示:
1: the driver of the 1st vehicle hit the insuredvehicle insuredvehicle at a stop
如果能帮我实现这个功能,我将不胜感激!
最佳答案
也许如果您在正则表达式中包含单词边界,而不是从替换中删除空格?当您只需要一个与模式匹配的完整单词而不是部分单词,同时避免这些空格问题时,这是理想的选择。\\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
关于r - Stringr str_replace_all 遗漏重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67575754/
来自 ?stringr::str_extract , 我试过 library(stringr) str_extract("number 123", "\\d") # [1] "1" 但我想要完整
给定的 str1 <- "0 1 1 2 2 3 3 4 0 4" 我想要: str2 <- "0 1\n1 2\n2 3\n3 4\n0 4" 用stringr做到这一点的方法是什么? 最佳答案 我
考虑这个简单的例子 dataframe dataframe # A tibble: 2 x 1 text 1
对于每个标题,我正在寻找一种方法来删除连字符两侧的前导零。 codes % str_replace("^0+(?!$)", "") %>% str_c(collapse="-") 并且很好奇是
首先,我对 R 和一般编程都很陌生,所以如果这是一个愚蠢的问题,我深表歉意。 我有一个与此类似的字符向量: > vec vec [1] "XabcYdef" "XabcYdef" "XabcYdef
This question already has answers here: Regular expression in R to remove the part of a string after
这是我的数据: df % mutate(X = A %>% str_replace_all(c(value1 = "m", value2 = "n"))) 我想要的输出是: df %>% mutate
对于每个标题,我正在寻找一种方法来删除连字符两侧的前导零。 codes % str_replace("^0+(?!$)", "") %>% str_c(collapse="-") 并且很好奇是
我最近将 ubuntu 从 18.04 升级到 20.04,所以这可能是相关的...... 我的代码要求我拥有 stringr 包并使用 tidyverse。当我尝试运行 require(string
我正在寻找从字符串中提取年份。这总是在“X”之后和“。”之前。然后是一串其他字符。 使用 stringr的 str_extract我正在尝试以下操作: year = str_extract(strin
我正在尝试编写一个函数来检测全部大写的大写单词 目前,代码: df % add_row(title= "THIS is an EXAMPLE where I DONT get the
我在使用 stringr::str_replace_all 函数时遇到问题。我正在尝试将 iv 的所有实例替换为 insuredvehicle,但该函数似乎只包含第一个术语。 temp_data <-
我有一个 dataframe的用户,其中一列包含他们 self 报告的位置。因此,报告的某些位置是无意义的,但在将此列与已知位置的其他列匹配时可能会导致误报。下面是数据框的示例。 data <- da
stringr有这种奇怪的行为,这真的很烦人。 stringr 会在没有警告的情况下更改某些包含外来字符的字符串的编码,在我的例子中是 ø、å、æ、é 和其他一些...如果您 str_trim 一个向
我知道我可以很容易地写一个,但是有谁知道 stringr(或 stringi)是否已经有一个函数连接一个或多个单词的向量,这些单词用逗号分隔,但在最后一个单词前有一个“and”? 最佳答案 您可以使用
Android 提供 support annotations ,而且我对 @StringRes 系列注释特别感兴趣,这些注释用于标记整数参数,迫使您传递有效资源而不是任何随机值。 使用 Android
我有一个看起来像这样的字符串向量,我想将其拆分: str <- c("Fruit LoopsJalapeno Sandwich", "Red Bagel", "Basil LeafBarbeque S
我在安装 stringr 时遇到问题。这是我请求安装 stringr 时得到的结果: utils:::menuInstallPkgs() also installing the dependency
我正在尝试将非捕获组与 str_extract 一起使用来自 stringr 的函数包裹。下面是一个例子: library(stringr) txt <- "foo" str_extract(txt,
perl()函数在最新版本的 stringr 中被弃用,取而代之的是 regex() .但是,我似乎无法复制早期的行为。 要大写字符串向量的第一个字母,这曾经有效: name <- c("jim",
我是一名优秀的程序员,十分优秀!