Skip to contents

This function makes "or" regex patterns from vectors or lists of strings. This function can be used with the matsbyname::select_rows_byname() and matsbyname::select_cols_byname functions. make_or_pattern() correctly escapes special characters in strings, such as ( and ), as needed. Thus, it is highly recommended that make_or_pattern be used when constructing patterns for row and column selections with matsbyname::select_rows_byname() and matsbyname::select_cols_byname().

Usage

make_or_pattern(
  strings,
  pattern_type = c("exact", "leading", "trailing", "anywhere", "literal")
)

Arguments

strings

A vector of row and column names.

pattern_type

One of "exact", "leading", "trailing", "anywhere", or "literal". Default is "exact".

Value

An "or" regex pattern suitable for selecting row and column names. Amenable for use with matsbyname::select_rows_byname or matsbyname::select_cols_byname.

Details

pattern_type controls the type of pattern created:

  • exact produces a regex pattern that selects row or column names by exact match.

  • leading produces a regex pattern that selects row or column names if the item in strings matches the beginnings of row or column names.

  • trailing produces a regex pattern that selects row or column names if the item in strings matches the ends of row or column names.

  • anywhere produces a regex pattern that selects row or column names if the item in strings matches any substring of row or column names.

  • literal returns strings unmodified, and it is up to the caller to formulate a correct regex.

Examples

make_or_pattern(strings = c("a", "b"), pattern_type = "exact")
#> [1] "^a$|^b$"
make_or_pattern(strings = c("a", "b"), pattern_type = "leading")
#> [1] "^a|^b"
make_or_pattern(strings = c("a", "b"), pattern_type = "trailing")
#> [1] "a$|b$"
make_or_pattern(strings = c("a", "b"), pattern_type = "anywhere")
#> [1] "a|b"
make_or_pattern(strings = c("a", "b"), pattern_type = "literal")
#> [1] "a" "b"