re — 0.2.0

org.pycforge.re · Author: PyCForge

License: GPL-3.0-only · Created: 2026-11-18

Runtime conversion · Direct declared calls

Using this import · Functions · Conversion contract · Worked examples

Function index

compile · escape · findall · finditer · fullmatch · match · search · split · sub · subn

Overview

Search and transform Unicode text or immutable bytes using Python-style regular expressions.

Using this import

Compile a reusable pattern or call a module function with the pattern and subject. Test a match against None before reading its captures. Use raw strings for patterns and templates.

Author's limitations

Use matching str or immutable bytes patterns and subjects. Text spans count Unicode code points. Binary spans count bytes, with ASCII categories and case folding. The profile supports positive and negative lookahead and fixed-width lookbehind, named and numbered pattern backreferences, captures, template and callable replacements, and the text profile's flags. UNICODE applies only to str. Common patterns use the ordered matcher. Capture-dependent patterns use bounded backtracking. Both paths charge Runtime work and memory. Budget exhaustion is fatal. Invalid syntax raises re.error. Limits are 4096 pattern units, 4096 instructions, 32 captures, 48 nested groups and 32 nullable repeated subexpressions. Pattern.match requires pos <= endpos after clamping. Mutable buffer subjects, conditionals, atomic groups, possessive repetitions, named Unicode escapes, DEBUG and LOCALE are outside this profile.

Release notes

Added lookaround, capture backreferences and immutable byte patterns.

Conversion contract

Use *import re in the module preamble. Each signature states accepted positions, names and defaults. A slash marks positional-only parameters; a star marks keyword-only parameters. Argument expansion, function values and Typed conversion are unsupported.

Lookahead checks the following input. Lookbehind checks a fixed-width preceding region. Positive assertions can capture values. Refer to a previous capture in a pattern by number or by name. For binary data, use a bytes pattern and a bytes subject. Spans then count bytes, and capture and replacement values are bytes.

Pattern and Match objects

Compiled patterns provide match, fullmatch, search, findall, finditer, split, sub and subn. Match objects provide group, groups, groupdict, start, end and span. Unmatched groups default to None and have span (-1, -1).

Flags: ASCII (A), IGNORECASE (I), MULTILINE (M), DOTALL (S), UNICODE (U), VERBOSE (X) and NOFLAG. Combine flags with |. Catch invalid patterns with except re.error.

re.compile(pattern, flags=0) -> regex-pattern

Compile a str or bytes pattern for reuse.

Normalized semantic slots: pattern, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: flags

Returns: regex-pattern

View example: capture-id

re.escape(pattern) -> regex-string-or-bytes

Escape metacharacters in literal text or bytes.

Normalized semantic slots: pattern. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Returns: regex-string-or-bytes

View example: rewrite-text

re.findall(pattern, string, flags=0) -> regex-results

Return whole matches or captured strings.

Normalized semantic slots: pattern, string, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: string

Argument 3: flags

Returns: regex-results

View example: scan-fields

re.finditer(pattern, string, flags=0) -> runtime-iterator

Iterate over successive match objects.

Normalized semantic slots: pattern, string, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: string

Argument 3: flags

Returns: runtime-iterator

View example: binary-captures

re.fullmatch(pattern, string, flags=0) -> regex-match-or-none

Match the entire string.

Normalized semantic slots: pattern, string, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: string

Argument 3: flags

Returns: regex-match-or-none

View example: capture-id

re.match(pattern, string, flags=0) -> regex-match-or-none

Match at the beginning of the string.

Normalized semantic slots: pattern, string, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: string

Argument 3: flags

Returns: regex-match-or-none

View example: capture-id

re.search(pattern, string, flags=0) -> regex-match-or-none

Find the first matching position.

Normalized semantic slots: pattern, string, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: string

Argument 3: flags

Returns: regex-match-or-none

View example: capture-id

re.split(pattern, string, maxsplit=0, flags=0) -> regex-results

Split and include captured separators.

Normalized semantic slots: pattern, string, maxsplit, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: string

Argument 3: maxsplit

Argument 4: flags

Returns: regex-results

View example: scan-fields

re.sub(pattern, repl, string, count=0, flags=0) -> regex-string-or-bytes

Replace matches with a template or Runtime function.

Normalized semantic slots: pattern, repl, string, count, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: repl

Argument 3: string

Argument 4: count

Argument 5: flags

Returns: regex-string-or-bytes

View example: rewrite-text

re.subn(pattern, repl, string, count=0, flags=0) -> regex-results

Return the substituted text and count.

Normalized semantic slots: pattern, repl, string, count, flags. These slots are supplied by the engine; the signature above defines source arguments and defaults.

Argument 1: pattern

Argument 2: repl

Argument 3: string

Argument 4: count

Argument 5: flags

Returns: regex-results

View example: rewrite-text

Worked examples

Compare your run with the expected output below. The SDK checks conversion when authoring a package. Opening Help and registering a package do not execute the examples.

Example: binary-captures

*import re as rx
def main():
    frame = b"\xff:AA:BB:AB"
    header = rx.compile(rb"(?<=:)(?P<byte>[A-Z])(?P=byte)(?=:|$)")
    print([(m.group("byte"), m.span()) for m in rx.finditer(header, frame)])
    return 0

Expected standard output:

[(b'A', (2, 4)), (b'B', (5, 7))]

Expected exit code: 0

Example: capture-id

*import re as rx
def main():
    pattern = rx.compile(r"(?P<id>[0-9]+)")
    hit = rx.search(pattern, "é/PCF-42")
    print(hit.group("id"), hit.span("id"))
    print(rx.match(pattern, "42b").group(), rx.fullmatch(pattern, "42b") is None)
    return 0

Expected standard output:

42 (6, 8)
42 True

Expected exit code: 0

Example: rewrite-text

*import re as rx
def main():
    literal = rx.escape("a+b")
    print(rx.sub(literal, "sum", "a+b"))
    print(rx.subn(r"([0-9]+)", r"<\1>", "part42"))
    return 0

Expected standard output:

sum
('part<42>', 1)

Expected exit code: 0

Example: scan-fields

*import re as rx
def main():
    text = "id:42 qty:7"
    print(rx.findall(r"\d+", text))
    print([m.span() for m in rx.finditer(r"\d+", text)])
    print(rx.split(r"\s+", text))
    return 0

Expected standard output:

['42', '7']
[(3, 5), (10, 11)]
['id:42', 'qty:7']

Expected exit code: 0