r-lib / rex

Friendly regular expressions for R.
https://rex.r-lib.org
Other
333 stars 27 forks source link

none_of("]") #51

Closed krlmlr closed 7 years ago

krlmlr commented 7 years ago

has a useless backslash. The closing bracket ] needs to be first after ^, without backslash.

library(rex)
rex_mode()
#> Rex functions and shortcuts attached!
rx <- none_of("]")
rx
#> [^\]]
grep(rx, "]")
#> integer(0)
grep(rx, "\\")
#> integer(0)
grep(rx, "a")
#> integer(0)
grep(rx, "]]")
#> [1] 1
rx <- "[^]]"
grep(rx, "]")
#> integer(0)
grep(rx, "\\")
#> [1] 1
grep(rx, "a")
#> [1] 1
grep(rx, "]]")
#> integer(0)

Tested with b037a54.

jimhester commented 7 years ago

Rex generates PCRE which support escaping characters inside character classes not POSIX regular expressions, which do not [1]. This is one of the reasons re_matches() sets perl = TRUE when it calls gregexpr() and regexpr().

library(rex)
rx <- rex(none_of("]"))
rx
#> [^\]]
grepl(rx, "]", perl = T)
#> [1] FALSE
grepl(rx, "a", perl = T)
#> [1] TRUE
re_matches("]", rx)
#> [1] FALSE
re_matches("a", rx)
#> [1] TRUE
krlmlr commented 7 years ago

Thanks for clarifying.