Skip to contents

Return which regular expression of several matches a character value

Usage

which_regex_matches(
  pattern,
  x,
  ignore.case = FALSE,
  perl = TRUE,
  fixed = FALSE,
  useBytes = FALSE
)

Arguments

pattern

The character vector with regular expressions to match against

x

The text to match against the regular expressions

ignore.case

Whether to be case sensitive (see base::grepl()).

perl

Whether pattern specifies Perl regexes (see base::grepl()).

fixed

Whether pattern is a regex or fixed string (see base::grepl()).

useBytes

See base::grepl().

Value

A logical vector or array (of x has more than one element)

Examples

pattern <-
  c("[0-9]",
    "[A-Z]",
    "[a-z]");

rock::which_regex_matches(
  pattern,
  "42"
);
#> [0-9] [A-Z] [a-z] 
#>  TRUE FALSE FALSE 

rock::which_regex_matches(
  pattern,
  "forty-two"
);
#> [0-9] [A-Z] [a-z] 
#> FALSE FALSE  TRUE 

rock::which_regex_matches(
  pattern,
  c(42, "forty-two")
);
#>          42 forty-two
#> [0-9]  TRUE     FALSE
#> [A-Z] FALSE     FALSE
#> [a-z] FALSE      TRUE