Specifies how many times a pattern should occur. This function acts as a quantifier, allowing for exact counts, ranges, or unbounded "many" matches.

xp_op_repeat(pattern, n, max = NULL)

Arguments

pattern

A regexpert object or character string from the pipe.

n

Integer. The minimum number of repetitions.

max

Integer or Inf. The maximum number of repetitions. If NULL (default), the pattern must match exactly n times.

Value

A regexpert object.

Details

The function automatically optimizes common regex quantifiers:

  • 0 to Inf becomes *

  • 1 to Inf becomes +

  • Exact counts use {n}

It also wraps the incoming pattern in a non-capturing group (?:) to ensure the quantifier applies to the entire preceding logic rather than just the last character.

Examples

# Match exactly 3 digits
xp_build_digits() %>% xp_op_repeat(3)
#> <regexpert pattern>
#>   (?:\d){3}

# Match between 2 and 5 lowercase letters
xp_build_letters(case = "lower") %>% xp_op_repeat(2, 5)
#> <regexpert pattern>
#>   (?:[a-z]){2,5}

# Match 1 or more alphanumeric characters
xp_build_alnum() %>% xp_op_repeat(1, Inf)
#> <regexpert pattern>
#>   (?:[A-Za-z0-9])+