regexp
).
glob2rx(pattern, trim.head = FALSE, trim.tail = TRUE)
"^.*"
should be
trimmed from the result.".*$"
should be
trimmed from the result.pattern
where each wildcard is translated to the corresponding
regular expression.
?
is mapped to .
(match a single
character), *
to .*
(match any string, including an
empty one), and the pattern is anchored (it must start at the
beginning and end at the end). Optionally, the resulting regexp is
simplified. Note that now even (
, [
and {
can be used
in pattern
, but glob2rx()
may not work correctly with
arbitrary characters in pattern
.
regexp
about regular expression,
sub
, etc about substitutions using regexps.
stopifnot(glob2rx("abc.*") == "^abc\\.",
glob2rx("a?b.*") == "^a.b\\.",
glob2rx("a?b.*", trim.tail = FALSE) == "^a.b\\..*$",
glob2rx("*.doc") == "^.*\\.doc$",
glob2rx("*.doc", trim.head = TRUE) == "\\.doc$",
glob2rx("*.t*") == "^.*\\.t",
glob2rx("*.t??") == "^.*\\.t..$",
glob2rx("*[*") == "^.*\\["
)
Run the code above in your browser using DataLab