Turtle

Library › pattern — regular expressions

pattern — regular expressions

import pattern

Find, check and replace text with regular expressions: patterns that describe what text looks like.

Functions

matches, findall, replaceall, splitby, groups.

matches[text, pattern]

Whether the pattern is found anywhere in text (^ and $ for all of it).

Example

code = "ABC-1234"
if ] matches[code, `^[A-Z]{3}-\d{4}$`] [
    show "valid code" .
if [end]

Output

valid code

findall[text, pattern]

Every match, in order.

Example

show findall["a1 b22", `\d+`] .

Output

[ "1", "22" ]

replaceall[text, pattern, with]

Replaces every match.

Example

show replaceall["2026-10-06", `(\d+)-(\d+)-(\d+)`, `$3/$2/$1`] .

Output

06/10/2026

splitby[text, pattern]

Splits text wherever the pattern matches.

Example

show splitby["a, b;c", `[,;]\s*`] .

Output

[ "a", "b", "c" ]

groups[text, pattern]

The parts in ( ) of the first match; a group that took no part in it is "".

Example

show groups["2026-10-06", `(\d+)-(\d+)`] .

Output

[ "2026", "10" ]
Top