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
compile · escape · findall · finditer · fullmatch · match · search · split · sub · subn
Search and transform Unicode text or immutable bytes using Python-style regular expressions.
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.
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.
Added lookaround, capture backreferences and immutable byte patterns.
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.
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.
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
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
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
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
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
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
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
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
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
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
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.
*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
*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
*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
*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