Skip to content

"matches" misinterprets regular expressions from rex #373

@DarwinAwardWinner

Description

@DarwinAwardWinner

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions