R:如何使Parse()接受逃脱字符的正则表达式?

发布于 2025-01-30 06:23:06 字数 1689 浏览 3 评论 0原文

我正在尝试使用验证器如果我的数据表中的某些行是否包含正则表达式。

我用要测试的列制作一个向量(字段),然后将验证器规则的命令粘合在一起。

为了能够在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝色星空 2025-02-06 06:23:06

我们可以使用\\

cmds <- paste("validator(",
                 paste(
                   map_chr(
                     fields, function(x) 
   paste0("grepl('^-?[0-9]\\\\d*(\\\\.\\\\d+)?

- 测试

> rule <- eval(parse(text = cmds))
> 
>  out <- confront(data, rule)
> out
Object of class 'validation'
Call:
    confront(dat = data, x = rule)

Rules confronted: 2
   With fails   : 1
   With missings: 0
   Threw warning: 0
   Threw error  : 0
, as.character(`", x, "`))") ), collapse = ","), ")")

- 测试

We may escape it with \\

cmds <- paste("validator(",
                 paste(
                   map_chr(
                     fields, function(x) 
   paste0("grepl('^-?[0-9]\\\\d*(\\\\.\\\\d+)?

-testing

> rule <- eval(parse(text = cmds))
> 
>  out <- confront(data, rule)
> out
Object of class 'validation'
Call:
    confront(dat = data, x = rule)

Rules confronted: 2
   With fails   : 1
   With missings: 0
   Threw warning: 0
   Threw error  : 0
, as.character(`", x, "`))") ), collapse = ","), ")")

-testing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文