Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-检查字符串是否只包含某些模式_R - Fatal编程技术网

R-检查字符串是否只包含某些模式

R-检查字符串是否只包含某些模式,r,R,我正在尝试设置一个表,用户可以在其中以字符串的形式插入公式 允许只使用操作符和给定的PAR名称(例如, PAR。名称< P>创建一个保持有效字符串串的向量: Allowed = c("+","-","*", "/","P","n","X") 编写一个函数,检查命令中是否有任何字符是不允许的: Vcheck = function(string){ tem

我正在尝试设置一个表,用户可以在其中以字符串的形式插入公式


允许只使用操作符和给定的PAR名称(例如,<代码> PAR。名称< P>创建一个保持有效字符串串的向量:

Allowed = c("+","-","*", "/","P","n","X")
编写一个函数,检查命令中是否有任何字符是不允许的:

Vcheck = function(string){
  temp = strsplit(string, "")
  (((temp %in% Allowed) %>% sum) == length(temp))  %>% return 
}

如果包含允许的字符,则返回1;如果包含禁止的字符,则返回0。我相信您知道如何编写函数来返回错误。

也许您可以尝试下面的代码

all(unlist(strsplit(s,"[^[:alnum:]]")) %in% par.names)
范例

> s1 <- "P1+P2*P3"

> s2 <- "P1+P5*P$4"

> all(unlist(strsplit(s1,"[^[:alnum:]]")) %in% par.names)
[1] TRUE

> all(unlist(strsplit(s2,"[^[:alnum:]]")) %in% par.names)
[1] FALSE
<代码> > S1 S2 ALL(unStRIST(S1,[[^:AlnUM:]))%%PAR中的名称) [1] 真的 >所有(unStRIST(S2,[[^::AlnU::]))%在%PAR中。 [1] 假的
这里有一种方法,首先定义变量名,然后给出一个与运算符匹配的正则表达式,然后我们可以生成一个validater函数

par.names<- c("P1", "P2", "P3")
operators <- "[*+]"

get_checker <- function(par.names, operatorsRE) {
  function(x) {
    nonops <- strsplit(x, operatorsRE)  
    sapply(nonops, function(x) all(x %in% par.names))
  }
}

checker <- get_checker(par.names, operators)

par.names谢谢你的回复。strsplit中的split参数应该是什么?另外,你认为这对长度大于1(例如“P1”)的允许项有效吗?@emantz参数是用户作为输入给出的命令-例如,如果用户给出字符串“P1+P2/2”`,您将其输入函数,就这样。@emantz我还注意到我遗漏了一个括号,现在已修复。我的意思是strsplit(string)中的split参数:strsplit(string)中的错误:缺少参数“split”,没有默认值。我想我需要添加[[1]]在temp之后,因为它是一个列表:So,所以它只能使用1个字符的允许元素。例如,使用Allowed=c(“+”、“-”、“*”、“/”、“P1”、“n”、“X”)、Vcheck(string=“X+P1”),将返回FALSE。请注意,对于
s,这将返回
TRUE
。如果OP对正则表达式一无所知,则有一个小小的改进,就是将运算符作为字符串,然后
paste0brackets@AbdessabourMtk.如果他们做了像“^*+”这样的事情,那还是很危险的。我不确定是否有一个函数会自动转义正则表达式中的“特殊”字符,但如果存在,这是一个好主意。如果
paste0(“[\\”,运算符“])
即使第一个操作符是
+
*
…在编辑之后,最好使用
折叠=''
,因为它们已经在字符内部class@AbdessabourMtk我刚刚把它从一个字符类改成了一个组,允许使用多字符操作符,比如
%%
之类的手势。
input <- c("P1+P2*P3", "P1+P5*P$4")
checker(input)
# [1]  TRUE FALSE
par.names<- c("P1", "P2", "P3")
operators <- c("^", "*","+")

get_checker <- function(par.names, operators) {
  operatorsRE <- paste0("(", paste(Hmisc::escapeRegex(operators), collapse="|"),")")
  function(x) {
    nonops <- strsplit(x, operatorsRE)  
    sapply(nonops, function(x) all(x %in% par.names))
  }
}