glob2rx {utils}R Documentation

Change Wildcard or Globbing Pattern into Regular Expression


Change wildcard aka globbing patterns into the corresponding regular expressions (regexp).

This is also a practical didactical example for the use of sub() and regular expressions.


glob2rx(pattern, trim.head = FALSE, trim.tail = TRUE)



character vector


logical specifying if leading "^.*" should be trimmed from the result.


logical specifying if trailing ".*$" should be trimmed from the result.


This takes a wildcard as used by most shells and returns an equivalent 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, for example escaped special characters.


A character vector of the same length as the input pattern where each wildcard is translated to the corresponding regular expression.


Martin Maechler, Unix/sed based version, 1991; current: 2004

See Also

regexp about regular expression, sub, etc about substitutions using regexps. Sys.glob does wildcard expansio, i.e., “globbing” on file paths more subtly, e.g., allowing to escape special characters.


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("*[*")  == "^.*\\["

[Package utils version 4.4.1 Index]