问题
I recently implemented a function to escape characters interpretable as regex that go into a system call for my R package 'rNOMADS'
SanitizeWGrib2Inputs <- function(check.strs) {
#Escape regex characters before inputting to wgrib2
#INPUTS
# CHECK.STRS - Strings possibly containing regex metacharacters
#OUTPUTS
# CHECKED.STRS - Strings with metacharacters appropriately escaped
meta.chars <- paste0("\\", c("(", ")", ".", "+", "*", "^", "$", "?", "[", "]", "|"))
for(k in 1:length(meta.chars)) {
check.strs <- stringr::str_replace(check.strs, meta.chars[k], paste0("\\\\", meta.chars[k]))
}
checked.strs <- check.strs
return(checked.strs)
}
and I include an example in my package documentation:
check.strs <- c("frank", "je.rry", "har\\old", "Johnny Ca$h")
checked.strs <- SanitizeWGrib2Inputs(check.strs)
This works fine on my ubuntu machine and passed the CRAN checks. However, when I uploaded the package to CRAN, their windows checker said:
> check.strs <- c("frank", "je.rry", "har\\old", "Johnny Ca$h")
> checked.strs <- SanitizeWGrib2Inputs(check.strs) Error in stri_replace_first_regex(string, pattern, fix_replacement(replacement), : Invalid capture group name. (U_REGEX_INVALID_CAPTURE_GROUP_NAME) Calls: SanitizeWGrib2Inputs -> <Anonymous> -> stri_replace_first_regex -> .Call Execution halted
** running examples for arch 'x64' ... ERROR Running examples in 'rNOMADS-Ex.R' failed The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: SanitizeWGrib2Inputs
> ### Title: Make sure regex metacharacters are properly escaped
> ### Aliases: SanitizeWGrib2Inputs
> ### Keywords: internal
>
> ### ** Examples
>
>
> check.strs <- c("frank", "je.rry", "har\\old", "Johnny Ca$h")
> checked.strs <- SanitizeWGrib2Inputs(check.strs) Error in stri_replace_first_regex(string, pattern, fix_replacement(replacement), : Invalid capture group name. (U_REGEX_INVALID_CAPTURE_GROUP_NAME) Calls: SanitizeWGrib2Inputs -> <Anonymous> -> stri_replace_first_regex -> .Call Execution halted
I verified this behavior on my Windows partition. What is the work around for this?
回答1:
Use a simple str_replace_all
to escape all the special regex metacharacters:
SanitizeWGrib2Inputs <- function(check.strs) {
return(str_replace_all(check.strs, "[{\\[()|?$^*+.\\\\]", "\\$0"))
}
check.strs <- c("frank", "je.rry", "har\\old", "Johnny Ca$h")
checked.strs <- SanitizeWGrib2Inputs(check.strs)
checked.strs
## => [1] "frank" "je\\.rry" "har\\\\old" "Johnny Ca\\$h"
Notes:
- The
"[{\\[\\]()|?$^*+.\\\\]
(actually,"[{\[\]()|?$^*+.\\]
) will match any single char, either{
,[
,]
,(
,)
,|
,?
,$
,^
,*
,+
,.
or\
- The
"\\$0"
replacement will change each of the chars into\
+ the same char (the$0
is the backreference to the whole match value). - I do not think you need to add
]
here since outside a character class it is not a special character if there is no paired opening[
in front.
来源:https://stackoverflow.com/questions/43001027/u-regex-invalid-capture-group-name-error-occurs-when-trying-to-escape-regex-char