英文:
Remove two or more consecutive characters using stringr
问题
我试图删除脚本中任何具有2个或更多连续ticks的时间。我从这里知道可以使用正则表达式(.)\1来检测任何重复字符,所以我将其修改为(`)\1,但这不起作用。为什么不起作用?
library(stringr)
example <- c("``", "````", "`")
str_replace_all(example, "(`)\", "gone") #希望前两个变成'gone',第三个保持不变
英文:
I'm trying to remove any time my script has 2 or more consecutive ticks. I know from here that using regex you can detect any repeating characters with (.)\1, so I modified it to (`)\1 but that doesn't work. Why not?
library(stringr)
example <- c("``", "````", "`")
str_replace_all(example, "(`)", "gone") #want the first 2 to say 'gone' and the 3rd to stay the same
答案1
得分: 3
library(stringr)
example <- c("``", "````", "`")
# 移除连续出现的一对
str_replace_all(example, "(``)+", "gone")
# 移除两个或更多连续的``
str_replace_all(example, "``+", "gone")
英文:
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")
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论