我正在尝试在正则表达式(Rstudio)中编写一个特殊字符列表,但它不适用于一两个。
我的列表是:“/\? * : [ ]”
例如:
library(tidyverse)
a <- "test:e?xample"
str_replace_all(a, "[/ \ ? * : [ ]]", "_")
[1] Output : "test_e_xample"
它有效!
但使用“[”则不然
a <- "test:e[xample"
str_replace_all(a, "[/ \ ? * : [ ]]", "_")
[1] Output : "test_e[xample"
最佳答案
在基本 R 中,在字符类中,元字符 "]"
必须位于第一个。
a <- "test:e?xample"
b <- "test:e[xample"
gsub("[][/\\?*:]", "_", a)
#> [1] "test_e_xample"
gsub("[][/\\?*:]", "_", b)
#> [1] "test_e_xample"
创建于 2022 年 10 月 3 日 reprex v2.0.2
关于r - 在正则表达式 R 中编写特殊字符列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73938687/