Validates that every non-NA element of a character value matches a
regular expression. Found: shows the first element that does not match
and At: lists every offender.
Arguments
- restriction
a
restrictionobject.- regex
character(1) regular expression (extended syntax, as in
grepl()).- fixed
logical; if
TRUE,regexis a literal substring.- ignore_case
logical; if
TRUE, matching ignores case. Not available withfixed = TRUE.
Details
A match anywhere in the string counts; anchor the pattern with ^
and $ to match the whole string. Non-character input fails with a type
error. NA elements are skipped; chain require_no_na() to reject them.
See also
Other character checks:
require_nchar(),
require_nonempty()
Examples
code <- restrict("code") |> require_character() |> require_pattern("^[A-Z]{3}$")
code(c("ABC", "XYZ"))
try(code(c("ABC", "xy")))
#> Error : code: must match pattern "^[A-Z]{3}$"
#> Found: "xy"
#> At: 2