Skip to contents

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.

Usage

require_pattern(restriction, regex, fixed = FALSE, ignore_case = FALSE)

Arguments

restriction

a restriction object.

regex

character(1) regular expression (extended syntax, as in grepl()).

fixed

logical; if TRUE, regex is a literal substring.

ignore_case

logical; if TRUE, matching ignores case. Not available with fixed = TRUE.

Value

The modified restriction object.

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