Skip to contents

This function takes a vector of strings and returns a concatenated regular expression pattern. It allows adding escaping before unescaped special characters and also add a prefix and sometimes a suffix to each elements before to concatenate them into a regex. The suffix will be added only for the texts with less characters than the n_char_to_add_suffix value (default never add a suffix).

Usage

get_regex_from_vector(
  vector,
  prefix_for_regex = "",
  suffix_for_regex = "",
  fix_escaping = TRUE,
  n_char_to_add_suffix = 0
)

Arguments

vector

character A character vector containing the elements to convert into a regular expression.

prefix_for_regex

character A string to prepend to each element of the vector in the regex pattern.

suffix_for_regex

character A string to append to each element of the vector in the regex pattern.

fix_escaping

logical If TRUE escaping will be fixed.

n_char_to_add_suffix

double, default = 0 The minimum number of characters for adding a suffix (default will never add a suffix)

Value

A single character string containing the regular expression pattern created by concatenating all elements from the input vector, with the specified prefix and suffix applied, and special characters escaped.

Details

such as a list of special_chars_to_escape, a number of escaping char. to add (default is num_escapes = 2), the escaping char. to consider, etc.

See also

Examples

if (FALSE) { # \dontrun{
# Example 1: Create a regex pattern for a vector of words
words <- c("apple", "banana", "cherry")
rgx <- get_regex_from_vector(words, "^", "$")

# Example 2: Create a regex pattern for words with escaped parentheses
words <- c("foo(bar)", "baz(qux)")
rgx <- get_regex_from_vector(words, "(", ")?", fix_escaping = TRUE)
#custom pattern "(" and ")?" will no be escaped, contrary to other texts
} # }