On this page:
rx/  optional
rx/  ?
rx/  zero-or-more
rx/  *
rx/  one-or-more
rx/  +
rx/  repeat
rx/  repeat-safe?
rx/  repeat-safely

4 Repetition🔗ℹ

Repetition allows for a single expression to match more than once, or not at all. There are multiple ways in which repetition may be indicated, using explicit procedures, using the #:repeat argument on group procedures, or the more specific arguments to the rx/repeat procedure. These options are compared in the following table.

rx/...

  

#:repeat

  

rx/repeat

rx/optional

  

optional

  

#:lower 0 #:upper 1

rx/zero-or-more

  

zero-or-more

  

#:lower 0 #:upper #f

rx/one-or-more

  

one-or-more

  

#:lower 1 #:upper #f

N/A

  

one

  

#:lower 1 #:upper 1

procedure

(rx/optional expr)  string?

  expr : string?

procedure

(rx/? expr)  string?

  expr : string?
This creates a new expression denoting that expr may be matched 0..1 times.

Examples:
> (rx/optional "maybe")

"maybe?"

> (rx/? "maybe")

"maybe?"

procedure

(rx/zero-or-more expr)  string?

  expr : string?

procedure

(rx/* expr)  string?

  expr : string?
This creates a new expression denoting that expr may be matched 0..∞ times. (Kleene star).

Examples:
> (rx/zero-or-more "maybe")

"maybe*"

> (rx/* "maybe")

"maybe*"

procedure

(rx/one-or-more expr)  string?

  expr : string?

procedure

(rx/+ expr)  string?

  expr : string?
This creates a new expression denoting that expr may be matched 1..∞ times. (Kleene Plus).

Examples:
> (rx/one-or-more "maybe")

"maybe+"

> (rx/+ "maybe")

"maybe+"

procedure

(rx/repeat expr [#:lower lower #:upper upper])  string?

  expr : string?
  lower : (or/c exact-nonnegative-integer? #f) = #f
  upper : (or/c exact-nonnegative-integer? #f) = #f
TBD

Examples:
> (rx/repeat "maybe" #:lower 1)

"maybe{1,}"

> (rx/repeat "maybe" #:upper 1)

"maybe{,1}"

> (rx/repeat "maybe" #:lower 1 #:upper 3)

"maybe{1,3}"

> (rx/repeat "maybe" #:lower 3 #:upper 1)

upper: contract violation

  expected: >= 3

  given: 1

> (rx/repeat "maybe")

upper: contract violation

  expected: or/c #f #f

  given: '(#f #f)

procedure

(rx/repeat-safe? expr)  boolean?

  expr : string?
TBD

Examples:
> (rx/repeat-safe? #\a)

#t

> (rx/repeat-safe? "(abc)")

#t

> (rx/repeat-safe? "[abc]")

#t

> (rx/repeat-safe? "maybe")

#f

procedure

(rx/repeat-safely expr    
  [#:lower lower    
  #:upper upper])  string?
  expr : string?
  lower : exact-nonnegative-integer? = 0
  upper : (or/c exact-nonnegative-integer? #f) = #f
TBD

Examples:
> (rx/repeat-safely #\a #:lower 1 #:upper 3)

"a{1,3}"

> (rx/repeat-safely "(abc)" #:lower 1 #:upper 3)

"(abc){1,3}"

> (rx/repeat-safely "[abc]" #:lower 1 #:upper 3)

"[abc]{1,3}"

> (rx/repeat-safely "maybe" #:lower 1 #:upper 3)

"(?:maybe){1,3}"