是否有 R 函数来转义正则表达式字符的字符串 [英] Is there an R function to escape a string for regex characters
问题描述
我想构建一个正则表达式来替换一些要搜索的字符串,因此需要先对这些字符串进行转义,然后才能将它们放入正则表达式中,这样如果搜索到的字符串包含正则表达式字符,它仍然有效.
I'm wanting to build a regex expression substituting in some strings to search for, and so these string need to be escaped before I can put them in the regex, so that if the searched for string contains regex characters it still works.
某些语言具有可以为您执行此操作的函数(例如 python re.escape
: https://stackoverflow.com/a/10013356/1900520).R有这样的功能吗?
Some languages have functions that will do this for you (e.g. python re.escape
: https://stackoverflow.com/a/10013356/1900520). Does R have such a function?
例如(组成函数):
x = "foo[bar]"
y = escape(x) # y should now be "foo\\[bar\\]"
推荐答案
我写了一个 R 版本的 Perl quotemeta
函数:
I've written an R version of Perl's quotemeta
function:
library(stringr)
quotemeta <- function(string) {
str_replace_all(string, "(\\W)", "\\\\\\1")
}
我总是使用 perl 风格的正则表达式,所以这对我有用.我不知道它是否适用于 R 中的正常"正则表达式.
I always use the perl flavor of regexps, so this works for me. I don't know whether it works for the "normal" regexps in R.
我找到了解释为什么这样做的来源.它位于 perlre 手册页的引用元字符部分:
I found the source explaining why this works. It's in the Quoting Metacharacters section of the perlre manpage:
这曾经在一个常见的习惯用法中用于禁用或引用要用于模式的字符串中正则表达式元字符的特殊含义.只需引用所有非单词"字符:
This was once used in a common idiom to disable or quote the special meanings of regular expression metacharacters in a string that you want to use for a pattern. Simply quote all non-"word" characters:
$pattern =~ s/(\W)/\\$1/g;
如您所见,上面的 R 代码是相同替换的直接翻译(在经历了反斜杠地狱之后).联机帮助页还说(强调我的):
As you can see, the R code above is a direct translation of this same substitution (after a trip through backslash hell). The manpage also says (emphasis mine):
与其他一些正则表达式语言不同,没有非字母数字的反斜杠符号.
Unlike some other regular expression languages, there are no backslashed symbols that aren't alphanumeric.
这强化了我的观点,即该解决方案仅适用于 PCRE.
which reinforces my point that this solution is only guaranteed for PCRE.
这篇关于是否有 R 函数来转义正则表达式字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!