Synsemadocsv0.6.xENES

Standard library / primitives

Text, Unicode normalization and regex (v0.6.60+)

text-patterns.syn
-- Text and pattern contracts introduced in engine v0.6.60. Pure, including WebAssembly.
intent: "doc example: normalize Unicode and choose bounded pattern matching explicitly"

print(backtrack_find_all("id: 12345;", "(?<=id: )\\d+"))

test "four normalization forms and byte equality"
    assert_eq(nfc("e\u0301"), "\u00e9")
    assert_eq(nfd("\u00e9"), "e\u0301")
    assert_eq(nfkc("\ufb01"), "fi")
    assert_eq(nfkd("\ufb01\u00e9"), "fie\u0301")
    assert("\u00e9" != "e\u0301")
    assert_eq(sha256(nfc("\u00e9")), sha256(nfc("e\u0301")))
    assert_eq(regex_replace(nfd("caf\u00e9"), "\\p{Mn}", ""), "cafe")
    assert_eq(nfkc("\ua7f1"), "S")

test "normalization requires text and bytes become unsigned integers"
    assert_eq(nfc(decode(bytes("e\u0301"))), "\u00e9")
    assert_error(() => nfc(bytes("x")))
    assert_error(() => nfd(1))
    assert_eq(to_list(bytes("h\u00f1")), [104, 195, 177])
    assert_error(() => to_list("hi"))
    assert_error(() => to_list([1, 2]))

test "partial searches versus whole-text matching"
    assert_eq(backtrack_find_all("id: 12345;", "(?<=id: )\\d+"), ["12345"])
    assert(backtrack_matches("123", "\\d+"))
    assert(not backtrack_matches("id: 123", "\\d+"))
    assert_eq(backtrack_find_all("zzz", "a(?=b)"), [])
    assert_eq(backtrack_capture("zzz", "a(?=b)"), nothing)
    assert_eq(backtrack_replace("zzz", "a(?=b)", "-"), "zzz")
    assert_eq(backtrack_find_all("ab", "a\\K(?=b)"), [""])

test "captures are always a list and unmatched groups are nothing"
    assert_eq(backtrack_capture("hello world", "w[a-z]+"), ["world"])
    assert_eq(backtrack_capture("b", "(a)?(b)"), [nothing, "b"])
    assert_eq(backtrack_capture("ab", "(?<left>a)(?P<right>b)"), ["a", "b"])
    assert_eq(backtrack_find_all("a1b2", "([0-9])"), ["1", "2"])

test "replacement templates use backslashes, not dollars"
    assert_eq(backtrack_replace("ab ab", "(a)(b)", "\\2-\\g<1>-$"), "b-a-$ b-a-$")
    assert_eq(backtrack_replace("ab", "(a)(b)", "$1"), "$1")
    assert_eq(backtrack_replace("b", "(a)?b", "<\\1><\\g<99>>"), "<><>")

test "literal backslash replacement escapes both the string and template"
    assert_eq(backtrack_replace("a", "a", "\\\\"), "\\")

test "choose the engine explicitly"
    assert_eq(regex_find_all("aaaa", "a++a"), ["aaaa"])
    assert_eq(backtrack_find_all("aaaa", "a++a"), [])
    assert_eq(backtrack_find_all("aaa", "(?U)a++"), ["aaa"])
    assert_eq(backtrack_find_all("ab", "a?a|a?b"), ["a", "b"])
    assert_eq(backtrack_capture("b", "(b?)*"), [""])
    assert_eq(backtrack_find_all("ab", "(?:a?|b)+"), ["a", ""])

test "backreferences, atomic groups, flags and group calls"
    assert_eq(backtrack_find_all("abab", "(ab)\\1"), ["abab"])
    assert_eq(backtrack_find_all("AA", "(?i)(a)\\1"), ["AA"])
    assert_eq(backtrack_find_all("abc", "(?>a)b(?=c)"), ["ab"])
    assert_eq(backtrack_find_all("abab", "(ab)\\g<1>"), ["abab"])
    assert_eq(backtrack_find_all("xyxz", "x(?=y\\g<-1>|z)"), ["x", "x"])
    assert_eq(backtrack_find_all("xyxz", "x(?=y\\g'-1'|z)"), ["x", "x"])

test "look-behind captures and keep-out have explicit boundaries"
    assert_eq(backtrack_capture("cde", "(?<=(ab|cd))e"), ["cd"])
    assert_eq(backtrack_capture("cb ab", "(?<!(a+))b"), [nothing])
    assert_eq(backtrack_capture("id: 12345;", "(\\d+)\\K;"), ["12345"])
    assert_eq(backtrack_find_all("id: 12345;", "(\\d+)\\K;"), [";"])
    assert_error(() => backtrack_find_all("id: 12345;", "(?<=(\\d+));"))
    assert_error(() => backtrack_find_all("abc", "(?<=\\K\\w)\\w"))
    assert_error(() => backtrack_find_all("abc", "(?<k>a\\Kb)(?=\\g<k>)"))
    assert_error(() => backtrack_replace("aa", "a(?=(?<=\\g<-1>.)|)", "-"))

test "empty conditional branches and unsupported constructs"
    assert_eq(backtrack_find_all("b", "(?:|(a))(?(1)|)b"), ["b"])
    assert_eq(backtrack_find_all("b", "(?:|(a))(?(1))b"), [])
    assert_eq(backtrack_find_all("b", "(?x)(?:|(a))(?(1) )b"), [])
    assert_error(() => backtrack_find_all("ab", "\\G\\w"))
    assert_error(() => backtrack_find_all("aaaaaa", "a{2}{3}"))
    assert_error(() => backtrack_find_all("aaa", "a{3,2}"))
    assert_error(() => backtrack_find_all("A", "(?i-u)a"))

test "the budget is named and errors preserve their message"
    assert_eq(backtrack_find_all("ab", "a(?=b)", max_steps = 1000), ["a"])
    assert_eq(backtrack_find_all("foo", "\\bfoo\\b", max_steps = 1), ["foo"])
    assert_error(() => backtrack_find_all("ab", "a(?!c)", max_steps = 1))
    assert_error(() => backtrack_find_all("ab", "a(?=b)", max_steps = 0))
    assert_error(() => backtrack_find_all(bytes("ab"), "a"))
    try
        backtrack_find_all("ab", "a(?!c)", max_steps = 1)
    recover e
        assert(contains(e, "backtrack_find_all: step limit exceeded"))
    try
        raise("source:12:3: keep the whole message")
    recover e
        assert_eq(e, "source:12:3: keep the whole message")

test "linear-family fixes and the legacy capture shape"
    assert_eq(regex_find_all("zabb", ".abb|b"), ["zabb"])
    assert_eq(regex_replace("zabb", ".abb|b", "-"), "-")
    assert(matches("a", "(?x)a # c"))
    assert_error(() => matches("ab", "a)(b"))
    assert_eq(regex_capture("world", "w[a-z]+"), ["world"])
    assert_eq(capture("world", "w[a-z]+"), "world")


test "named references, scoped flags, definitions and absent repeater"
    assert_eq(backtrack_find_all("abab", "(?<g>ab)\\k<g>"), ["abab"])
    assert_eq(backtrack_find_all("abab", "(?P<g>ab)(?P=g)"), ["abab"])
    assert_eq(backtrack_find_all("ab", "(?(DEFINE)(?<g>ab))(?P>g)"), ["ab"])
    assert_eq(backtrack_find_all("ab", "(?(DEFINE)(?<g>ab))\\g'g'"), ["ab"])
    assert_eq(backtrack_find_all("A", "(?i:a)"), ["A"])
    assert_eq(backtrack_find_all("xzzy xaby", "x(?~a\\Kb)y"), ["xzzy"])
    assert_error(() => backtrack_find_all("a", "(?~|a)"))

test "pattern length and compiled memory are separate from the step budget"
    let long_pattern be join(apply(range(16385), (i) => "a"), "")
    assert_error(() => backtrack_find_all("", long_pattern))
    assert_error(() => backtrack_find_all("a", "\\w{300}", max_steps = 10000000))

test "recursion depth fails to match rather than raising an error"
    let a25 be join(apply(range(25), (i) => "a"), "")
    let b25 be join(apply(range(25), (i) => "b"), "")
    let a20 be join(apply(range(20), (i) => "a"), "")
    let b20 be join(apply(range(20), (i) => "b"), "")
    assert_eq(backtrack_find_all(a25 + b25, "(a\\g<1>?b)"), [a20 + b20])
    assert(not backtrack_matches(a25 + b25, "(a\\g<1>?b)"))

All functions on this page are pure: no capability is required, and they also work in WebAssembly. Backslashes in a Synsema string must be doubled: the regex \d+ is written "\\d+". Patterns are text; there is no separate compiled-pattern value to construct.

Normalize before comparing, indexing or hashing§

CallResultUse
nfc(text)Canonical decomposition followed by compositionStore canonically equivalent spellings in one form
nfd(text)Canonical decompositionSeparate letters from combining marks
nfkc(text)Compatibility decomposition followed by compositionAlso fold compatibility characters such as ligatures
nfkd(text)Compatibility decompositionDecompose compatibility characters and marks

These functions accept text only and return text. For bytes, decode first: nfc(decode(b)). A number, list or bytes value is an error, not an implicit conversion. Normalization is not lowercasing or transliteration; compatibility normalization can erase meaningful distinctions, so choose the form for the data contract.

nfc("e\u0301") is "\u00e9"; nfkc("\ufb01") is "fi". regex_replace(nfd("caf\u00e9"), "\\p{Mn}", "") gives "cafe". Equality, map keys and hashes still compare the original bytes: "\u00e9" == "e\u0301" is false. Normalize both operands before comparing or hashing. The normalization tables are Unicode 17.0; regex properties such as \p{Mn} use Unicode 16.0. A character added in 17 can normalize here while still being unassigned for regex properties. This matters when reproducing another tokenizer's Unicode version.

to_list(bytes("h\u00f1")) returns [104, 195, 177]: unsigned byte values 0..255, not Unicode code points. It also accepts arrays as before; text, lists and other values are not accepted as a generic iterable conversion.

Choose the regex family explicitly§

TaskLinear familyBacktracking family (v0.6.60+)
Whole text matchesmatches(text, pattern)backtrack_matches(text, pattern)
Every non-overlapping whole matchregex_find_all(text, pattern)backtrack_find_all(text, pattern)
Captures from the first matchregex_capture(text, pattern)backtrack_capture(text, pattern)
Replace all non-overlapping matchesregex_replace(text, pattern, replacement)backtrack_replace(text, pattern, replacement)

Use the linear family for ordinary patterns. It has no look-around or pattern backreferences. Use backtrack_ for look-ahead/behind, pattern backreferences, possessive repetitions or atomic groups. There is no automatic engine switch. The same pattern can have different meanings: a++a is possessive in backtrack_ and cannot match aaaa; the linear family reads it as a nested repetition.

Arguments and results§

The four backtracking calls additionally accept named max_steps = N, an integer of at least 1. Example: backtrack_find_all("ab", "a(?=b)", max_steps = 1000). Do not pass the limit positionally. Text, pattern and replacement must all be text; use decode(b) for bytes. Unlike the linear functions, these do not stringify values.

has no anchors. It preserves calls to the whole pattern; do not manually add a wrapper to emulate it. For a partial existence check, use backtrack_capture(...) != nothing.

groups do not change that shape. Empty matches are allowed; the search advances by a Unicode character when necessary so it does not loop on an empty match.

returns their values in opening-parenthesis order; a group that did not participate is nothing. Without groups it returns [whole_match], never bare text.

template uses \\1, \\2 or \\g<1> in a Synsema string. $ is literal; $1 is not the group syntax. An unmatched or unknown group expands to empty text. To output a literal backslash, double it for the template as well as for the string: backtrack_replace("a", "a", "\\\\") produces one backslash.

Pattern syntax you can use§

Pattern notation (before Synsema string escaping)Meaning
(?=p) / (?!p)Positive / negative look-ahead; does not consume text
(?<=p) / (?<!p)Positive / negative look-behind; see capture restrictions below
(p) / (?:p)Capture / grouping without capturing
(?<name>p) / (?P<name>p)Named capture; results are still a list in group order
\1, \k<name>, (?P=name)Match what a group captured (a pattern backreference)
p*, p+, p?, p{m,n}; suffix ?Greedy repetition; lazy with the suffix
p*+, p++, p?+, p{m,n}+; (?>p)Possessive repetition / atomic group: do not backtrack into it
\KReset the reported start of the match; write it outside look-arounds
\g<name>, \g'name', (?P>name)Call a group's pattern, rather than match its captured text
\g<1>, \g<-1>, \g<0>Call a numbered group, a relative group, or the whole pattern
(?(1)yes|no); (?(DEFINE)(?<g>p))Branch on whether a capture participated; define a group to call
(?i), (?m), (?s), (?x), (?U); (?i:p)Case-insensitive, multiline, dot-all, verbose, swap greedy/lazy; scoped flag

Possessive repetition stays greedy even with (?U). Unicode is always enabled; (?i-u) is an error. (?~p) (absent repeater) is supported, but the absent expression, stopper and clear forms are not. \G is explicitly unsupported. Call syntax is not Perl's (?R), (?1) or (?&name). Do not assume every PCRE or Python pattern is accepted.

A backreference inside its own group reads that group's previous completed capture. A conditional there also asks whether it has completed before, not merely started. (?(1)|) keeps two empty branches and matches either way. (?(1)) (and an empty branch without a |, including whitespace in (?x) or a comment) is the condition alone: it continues only if the group participated. This last form differs from Perl/Python. Recursion deeper than 20 levels fails to match at that point; it is not an error. Use a parser for deep structured data instead of relying on regex recursion.

Rejected patterns and what to write instead§

error: (?<=(\d+)); as a regex has no single capture interpretation. When parentheses only group, use (?:...). To capture, consume outside it: the Synsema pattern "(\\d+)\\K;" captures the digits and reports only the semicolon as the match. A fixed-length capturing look-behind, a negative one, or a variable-length one with no captures/calls is allowed. This check applies where the look-behind is compiled; an unused DEFINE body or {0} may not be compiled.

and indirect calls to groups containing \K, and definitions nobody calls. Move \K outside the look-around. This rule checks the written pattern before optimization.

(?:a{2}){3}. A count with minimum greater than maximum (a{3,2}) is an error.

any capture, or \g<-2> after one). It has the same meaning as \g<0>; it cannot use a rewritten group 0 to change the match bounds from inside a look-around.

Budgets, errors and untrusted patterns§

max_steps is a deterministic work counter, not milliseconds and not just the number of backtracks. The budget belongs to the whole call, across all its searches. By default it is max(1048576, (64 + compiled_instruction_count) * text_byte_length). An instruction costs one step; matched/compared stretches, fixed-length backward walks, skipped newlines and initializing capture slots also cost one per 16 bytes/slots. A pattern delegated whole to the linear engine takes no steps, including a simple positive look-ahead at the very end ("a(?=b)"); a negative one ("a(?!c)") takes steps. Full-match wrapping can prevent that delegation. Some empty-matching unbounded repetitions and ambiguous alternations stay in the backtracking engine so that results are consistent, even without a look-around.

A step budget is not a time limit. Failed long scans, linear-engine work and building search tables can take time with few or no steps. A service accepting outside patterns should set its own max_steps and isolate/time-bound the request if it needs a wall-clock limit. The default grows with the pattern; it is not a boundary for hostile patterns.

A repetition retried from each position can be quadratic: \\d+(?![\\d.]) as a Synsema string scans a number repeatedly. Anchor where a match may start: "(?<!\\d)\\d+(?![\\d.])". Possessive suffixes alone do not remove repeated starting positions.

Limit / error fragmentWhat to change
step limit exceededAnchor starts; use possessive/atomic forms where correct; otherwise raise named max_steps
Pattern over 16 KiB (16384 bytes)Split it into smaller patterns
Compiled pattern and initial search memory over 16 MiBCapture less ((?:...)), use smaller counts or ASCII classes, split patterns
More than 1,000,000 pending backtrack pointsMake long repetitions possessive or atomic where that preserves the intended match
invalid regex patternFix syntax or the rejected construct; increasing max_steps does not fix compilation

Search tables have a separate per-pattern/thread budget: growth over 16 MiB during a call is dropped and rebuilt as needed; at its end at most 2 MiB is retained. Results and step counts do not depend on whether the tables were retained. These limits do not bound total process memory or promise a wall-clock deadline.

Catch errors with try / recover e. Since v0.6.60, e retains the whole error message; do not strip or parse a guessed file:line:column: prefix. An unexpected internal regex panic is a catchable internal error on native platforms; in WASM a panic is a trap. This is a bug-report path, not a normal invalid-pattern result.

Repeated calls and upgrading from v0.6.59§

Both families automatically keep compiled patterns per thread (no public cache API): at most 32 pattern/variant entries, with a full-match variant distinct from a search variant; patterns over 8 KiB and failed compilations are not cached. Each request, cron tick or socket handler under serve starts and ends with an empty cache; run keeps it for that run. An agent keeps its own while it runs on its thread. The 32/8-KiB limits count pattern source, not compiled memory: a cache filled with large compiled patterns can still occupy hundreds of MiB. A tokenizer pattern is typically much smaller. The two families retain at most 2 MiB of search state per compiled pattern between calls.

The linear family keeps its semantics, with two fixes: matches rejects an invalid pattern even if a wrapper would accidentally balance it ("a)(b"), and searches now correctly choose the leftmost match in cases such as regex_find_all("zabb", ".abb|b") which returns ["zabb"] (previously ["b", "b"]). A verbose final comment works: matches("a", "(?x)a # c") is true. Invalid-pattern errors name what the program wrote; a pattern valid alone but invalid after full-match wrapping reports that distinction. The old find_all, capture, replace_re names remain deprecated aliases; capture without capturing groups still returns bare match text, unlike regex_capture.