[英]How to precisely check for illegal characters of file paths?
我正在开发一个 Shiny 应用程序,它为每个用户及其每个实验生成文件夹和子文件夹。
我希望确保用户名和实验名都不包含任何非法字符。
我用我知道的每个非法字符定义了一个字符向量,但是,有可能出现人为错误。 有没有更精确的方法来做到这一点?
dir <- "~/home/my_app_data"
usr <- "john"
exp <- "explosion`s"
path <- paste(dir, usr, exp, sep = "/")
illegal <- c(" ", ",", "`")
if (any(illegal %in% (strsplit(x = path, split = "") %>% unlist))) {
stop( "Illegal characters used")
} else {
dir.create(path, recursive = T)
}
使用grepl
。 pattern="\\W"
查找不包括下划线"_"
的非单词字符。
FUN <- function(x) {
if (grepl("\\W", x)) stop(sprintf("Illegal characters used in '%s'", x)) else x
}
FUN(usr)
# [1] "john"
FUN(exp)
# Error in FUN(exp) : Illegal characters used in 'explosion`s'
lapply(c(usr, exp), FUN)
# Error in FUN(X[[i]], ...) : Illegal characters used in 'explosion`s'
FUN("john123")
# [1] "john123"
FUN("john_123")
# [1] "john_123"
(当然你想定义你的自定义else
条件。)
dir <- "~/home/my_app_data"
usr <- "john"
exp <- "explosion`s"
path <- paste(dir, usr, exp, sep = "/")
应该防止大多数错误:
if(!(identical(gsub(
"[^[:alnum:]]+",
"_",
iconv(exp, from = "ascii", "utf-8")
), exp))) {
stop("Illegal characters used")
} else {
dir.create(path, recursive = TRUE)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.