我正在使用正则表达式来替换一些子串.替换值重用了部分匹配.我希望不区分大小写,但在替换中,我想要匹配的小写版本.
library(stringi)
x <- "CatCATdog"
rx <- "(?i)(cat)(?-i)"
stri_replace_all_regex(x, rx, "{$1}")
# [1] "{Cat}{CAT}dog"
Run Code Online (Sandbox Code Playgroud)
这接近我想要的,除了"猫"应该是小写.也就是说,输出字符串应该是"{cat}{cat}dog".
以下代码不起作用,但它显示了我的意图.
stri_replace_all_regex(x, rx, "{tolower($1)}")
Run Code Online (Sandbox Code Playgroud)
以下技术确实有效,但它很难看,不是很普遍,也不是很有效.我的想法是用一个匹配我想要的正则表达式替换正则表达式,而不是替换值(即"cat"而不是"{cat}").然后在每个输入字符串中搜索第一个匹配项,找到匹配项的位置,执行子字符串替换,然后查找下一个匹配项,直到不再存在.太可怕了.
x <- "CatCATdog"
rx <- "(?i)((?<!\\{)cat(?!\\}))(?-i)"
repeat{
detected <- stri_detect_regex(x, rx)
if(!any(detected))
{
break
}
index <- stri_locate_first_regex(x[detected], rx)
match <- tolower(stri_match_first_regex(x[detected], rx)[, 2])
stri_sub(x[detected], index[, 1], index[, 2]) <- paste0("{", match[detected], "}")
}
Run Code Online (Sandbox Code Playgroud)
我觉得必须有更好的方法.
如何用小写值替换不区分大小写的匹配?
感谢评论的灵感,我发现我正在寻找的是" 替换文本案例转换 ".