-
Notifications
You must be signed in to change notification settings - Fork 41
Open
Description
I see that tidyselect::matches has some special code to distinguish different kinds of regular expression. However, this logic seems to be implemented as hard-coded checks for certain classes rather than a generic and methods. Since it doesn't account for the "regex" class from the rex package, it can fail, as demonstrated here:
library(stringr)
library(rex)
#>
#> Attaching package: 'rex'
#> The following object is masked from 'package:stringr':
#>
#> regex
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following object is masked from 'package:rex':
#>
#> matches
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
## Regular rex class: fails
my_regex <- rex("p" %if_next_isnt% "g")
class(my_regex)
#> [1] "regex"
mtcars %>% select(matches(my_regex)) %>% colnames
#> Warning in grep(needle, haystack, ...): TRE pattern compilation error 'Invalid
#> regexp'
#> Error in `select()`:
#> ℹ In argument: `matches(my_regex)`.
#> Caused by error in `grep()`:
#> ! invalid regular expression '(?:p(?!g))', reason 'Invalid regexp'
## Convert to stringr::regex: works
my_stringr_regex <- stringr::regex(my_regex)
#> Warning in stringr::regex(my_regex): Coercing `pattern` to a plain character
#> vector.
class(my_stringr_regex)
#> [1] "stringr_regex" "stringr_pattern" "character"
mtcars %>% select(matches(my_stringr_regex)) %>% colnames
#> [1] "disp" "hp"Created on 2025-09-23 with reprex v2.1.1
The short-term fix would seem to be to add "regex" to the list of classes treated specially on this line: https://github.com/r-lib/tidyselect/blob/main/R/helpers-pattern.R#L152
Long-term, perhaps it's possible to make matches a generic?
Metadata
Metadata
Assignees
Labels
No labels