函数Hmisc::escapeRegex转义字符串中的任何特殊字符。
library(Hmisc)
string <- "this\\(system) {is} [full]."
escapeRegex(string)它基于gsub和regexp。
escapestring <- gsub("([.|()\\^{}+$*?]|\\[|\\])", "\\\\\\1", string)
escapestring
[1] "this\\\\\\(system\\) \\{is\\} \\[full\\]\\."如何从escapestring中删除反斜杠以便检索原始的string?
发布于 2014-12-04 17:34:35
实际上,您只需要保留每个\之后的字符就可以不转义了。
string <- "this\\(system) {is} [full]."
library(Hmisc)
gsub("\\\\(.)", "\\1", escapeRegex(string))
#> [1] "this\\(system) {is} [full]."或者,雷克斯可能会使转义和未转义都变得简单一些。
library(rex)
re_substitutes(escape(string), rex("\\", capture(any)), "\\1", global = TRUE)
#> [1] "this\\(system) {is} [full]."发布于 2014-12-01 11:38:47
那判决呢?
\\\\([.|()\\^{}+$*?]|\\[|\\])用捕获组\1替换
示例用法
escapestring <- "this\\\\\\(system\\) \\{is\\} \\[full\\]\\."
string <- gsub("\\\\([.|()\\^{}+$*?]|\\[|\\])", "\\1", escapestring)
string
[1] "this\\(system) {is} [full]."发布于 2014-12-01 12:46:26
也许这也有帮助
gsub("\\\\[(](*SKIP)(*F)|\\\\", '', escapestring, perl=TRUE)
#[1] "this\\(system) {is} [full]."https://stackoverflow.com/questions/27227229
复制相似问题