glob2rx                package:utils                R Documentation

_C_h_a_n_g_e _W_i_l_d_c_a_r_d _o_r _G_l_o_b_b_i_n_g _P_a_t_t_e_r_n _i_n_t_o _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     Change _wildcard_ aka _globbing_ patterns into the corresponding
     regular expressions ('regexp').

_U_s_a_g_e:

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

_A_r_g_u_m_e_n_t_s:

 pattern: character vector

trim.head: logical specifying if leading '"^.*"' should be trimmed from
          the result.

trim.tail: logical specifying if trailing '".*$"' should be trimmed
          from the result.

_D_e_t_a_i_l_s:

     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'.

_V_a_l_u_e:

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

_A_u_t_h_o_r(_s):

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

_S_e_e _A_l_s_o:

     'regexp' about regular expression, 'sub', etc about substitutions
     using regexps.

_E_x_a_m_p_l_e_s:

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

