R:如何使Parse()接受逃脱字符的正则表达式?
我正在尝试使用验证器如果我的数据表中的某些行是否包含正则表达式。
我用要测试的列制作一个向量(字段),然后将验证器规则的命令粘合在一起。
为了能够在confront()函数中使用规则,我使用parse()和eval()将字符串转换为表达式,
以下示例按预期工作:
library(validate)
data <- data.frame("Protocol_Number" = c("123", "A122"), "Numeric_Result" = c("-0.5", "1.44"))
fields <- c("Protocol_Number", "Numeric_Result")
# build validator commands for each field
cmds <- paste("validator(",
paste(
map_chr(
fields, function(x) paste0("grepl('^-?[0-9]', as.character(`", x, "`))")
), collapse = ","),
")")
# convert to rule and do the tests
rule <- eval(parse(text = cmds))
out <- confront(data, rule)
summary(out)
但是,我想使用识别任何类型的正则示例与文本相对的数字,就像在此工作示例中
grepl('^-?[0-9]\\d*(\\.\\d+)?$', c(1, -1, 0.5, "Not Done"))
尝试使用此正则示例时,parse()函数会丢弃错误:
Error: '\d' is an unrecognized escape in character string starting "'^-?[0-9]\d"
这是不起作用的:
# build validator commands for each field
cmds <- paste("validator(",
paste(
map_chr(
fields, function(x) paste0("grepl('^-?[0-9]\\d*(\\.\\d+)?$', as.character(`", x, "`))")
), collapse = ","),
")")
# convert to rule and do the tests
rule <- eval(parse(text = cmds))
out <- confront(data, rule)
summary(out)
我如何使parse()接受逃脱的字符?还是有更好的方法可以做到这一点?
I am trying to use the validator package to check if certain rows in my data table contain a regular expression or not.
I make a vector (fields) with the columns I want to test and then paste together the commands for the validator rules as a string.
To be able to use the rules in the confront() function I use parse() and eval() to turn the character string into an expression
The following example is working as expected:
library(validate)
data <- data.frame("Protocol_Number" = c("123", "A122"), "Numeric_Result" = c("-0.5", "1.44"))
fields <- c("Protocol_Number", "Numeric_Result")
# build validator commands for each field
cmds <- paste("validator(",
paste(
map_chr(
fields, function(x) paste0("grepl('^-?[0-9]', as.character(`", x, "`))")
), collapse = ","),
")")
# convert to rule and do the tests
rule <- eval(parse(text = cmds))
out <- confront(data, rule)
summary(out)
However, I want to use a regex that recognizes any sort of number as opposed to text, like in this working example
grepl('^-?[0-9]\\d*(\\.\\d+)?
When I try to use this regex in the above example, the parse() function will throw an error:
Error: '\d' is an unrecognized escape in character string starting "'^-?[0-9]\d"
This is not working:
# build validator commands for each field
cmds <- paste("validator(",
paste(
map_chr(
fields, function(x) paste0("grepl('^-?[0-9]\\d*(\\.\\d+)?
How do I make parse() accept the escaped characters? Or is there a better way to do this?
, c(1, -1, 0.5, "Not Done"))
When I try to use this regex in the above example, the parse() function will throw an error:
This is not working:
How do I make parse() accept the escaped characters? Or is there a better way to do this?
, as.character(`", x, "`))")
), collapse = ","),
")")
# convert to rule and do the tests
rule <- eval(parse(text = cmds))
out <- confront(data, rule)
summary(out)
How do I make parse() accept the escaped characters? Or is there a better way to do this?
, c(1, -1, 0.5, "Not Done"))When I try to use this regex in the above example, the parse() function will throw an error:
This is not working:
How do I make parse() accept the escaped characters? Or is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
\\
- 测试
We may escape it with
\\
-testing