Repository navigation
Fix matches on empty matches, and match positions for patterns that use \K
Iterating over Regex.matches never ended once it reached an empty match, which patterns such as a*, x? and a multiline ^$ produce. The iterator now ends, and it reports each empty match once.
For a pattern that uses \K, Match.start_pos() returned the offset where PCRE2 began the match, not the start of the reported match. end_pos(), matches and split used that offset, so they gave wrong results, and iteration could resume inside a UTF-8 character. For example, Regex("a\\Ka")?.matches("aaa") returned two matches instead of one, and Regex("a\\Kb")?.split("xaby") returned ["x", "by"] instead of ["xa", "y"]. start_pos() and end_pos() now describe the reported match, for a Match from matches or from Regex.apply, and matches and split use those positions.
Add Match.end_pos_exclusive()
Match.end_pos_exclusive() returns the byte offset in the subject where a match ends. The match is the bytes from start_pos() up to, but not including, end_pos_exclusive(). Use it instead of end_pos() when a match can be empty: end_pos() returns start_pos() - 1 for an empty match, which wraps to USize.max_value() at offset 0.
match \exhaustive\ Regex("b+")?("abbc")
| let m: Match =>
m.start_pos() // 1
m.end_pos_exclusive() // 3
m.end_pos() // 2
m.dispose()
| RegexNoMatch => None
| let _: RegexMatchError => None
end
match \exhaustive\ Regex("x?")?("ab") // an empty match at offset 0
| let e: Match =>
e.start_pos() // 0
e.end_pos_exclusive() // 0
e.end_pos() // USize.max_value()
e.dispose()
| RegexNoMatch => None
| let _: RegexMatchError => None
endAdd compile options to Regex.create
Regex.create takes a new options parameter that changes how the pattern is compiled. Existing calls such as Regex(pattern)? and Regex(pattern, false)? do not change.
// Caseless matching
let r = Regex(pattern where options = [RegexCaseless])?
// Require that the whole subject matches
let number = Regex("\\d+" where options = [RegexAnchored; RegexEndAnchored])?
number.has_match("42") // true
number.has_match("42\n") // falseThere are five options. RegexCaseless, RegexMultiline and RegexDotAll set the same modes as the inline settings (?i), (?m) and (?s), but they also work for a pattern that starts with a setting such as (*UCP), where an added (?i) does not compile. RegexAnchored works like \G(?:...) around the whole pattern, and RegexEndAnchored makes a match end at the end of the subject. Together they require that the whole subject matches, which ^...$ does not, because $ also matches before a newline at the end.
A pattern compiled with RegexEndAnchored always uses PCRE2's interpreter, not JIT, because PCRE2's JIT can give wrong results for (*ACCEPT) with that option.
The library now requires PCRE2 10.35 or later.
Fix Regex.index failing to link
Any program that called Regex.index failed to link with an undefined symbol error. Programs that call Regex.index now link. Regex.index returns the capture index for the given name, or a RegexIndexError when no capture has that name, when more than one capture index has that name, or after Regex.dispose().
Fix Regex.split holding match data until garbage collection
Regex.split previously kept match data for every match until garbage collection ran after the calling behavior ended. Splitting a long string with many matches caused large memory growth, especially without JIT. Regex.split no longer accumulates match data across matches and releases its match data before returning.
Reject \C in regular expression patterns
Regex.create now raises an error when a pattern uses \C.
Previously, \C matched a single UTF-8 byte instead of a full character. On multi-byte UTF-8 subjects, a match could end inside a character. Match.apply, Regex.split, and Regex.replace then returned invalid UTF-8 strings. Regex.matches and Regex.split dropped later matches.
Replace \C with . to match one Unicode code point (add RegexDotAll or (?s) to include newlines). Use \X to match one extended grapheme cluster:
// Before: compiled and could split multi-byte UTF-8 characters.
// Now raises an error when compiling the pattern.
let r = Regex("\\C")?
// After: match one Unicode code point (including newlines).
let r = Regex("." where options = [RegexDotAll])?
// Or: match one extended grapheme cluster.
let r = Regex("\\X")?Regex.create has no option to enable raw single-byte matching.
Fix (*NO_JIT) matching, JIT UTF-8 and offset checks, and slow matches and split
Patterns that use (*NO_JIT) now match when jit is true. Previously, eq, ne, apply, matches, and split treated those patterns as non-matching when JIT was available and jit was true.
When jit is true, matching now rejects invalid UTF-8 subjects and out-of-range or mid-character offsets instead of returning wrong matches, reading past the subject, or crashing. JIT and non-JIT matching now reject the same invalid inputs.
Regex.matches and Regex.split previously took quadratic time on long subjects with many matches when JIT was disabled or unavailable. They no longer slow down quadratically as the number of matches grows.
Fix JIT stack limit failures on backtracking patterns
Regex matching and replacement no longer fail on backtracking patterns when jit is true and backtracking exceeds the JIT stack limit.
Previously, eq, ne, apply, matches, split, and replace treated subjects with around 1,000 repetitions as non-matching for patterns such as (a|b)*c and ^(\w+\s?)*$ when jit was true, even though the same calls matched when jit was false. has_match, apply, matches, split, and replace now return the same result whether jit is true or false.
Enable PCRE2 JIT support in Windows builds
Regex now uses JIT matching on Windows when jit is true.
Previously, .\make.ps1 libs, corral fetch, and corral update built PCRE2 on Windows without JIT support. Regex always ran without JIT even when jit was true.
When you upgrade an existing Windows checkout or dependent project, running .\make.ps1 libs, corral fetch, or corral update automatically rebuilds PCRE2 with JIT support.
Make global Regex.replace consistent across PCRE2 versions
Global Regex.replace now produces the same results across all supported PCRE2 versions, matching Regex.matches.
Previously, with PCRE2 10.46 and earlier, a global replace with a pattern that can match an empty string alongside \G, RegexAnchored, or a backtracking verb such as (*COMMIT) could continue replacing after an empty match instead of stopping.
Add RegexEscape.pattern to escape literal text for patterns
RegexEscape.pattern escapes arbitrary text so a Regex matches it literally, either as a whole pattern or embedded inside a larger pattern.
Previously, the library had no safe way to match user-supplied text literally. Wrapping untrusted text in \Q...\E breaks when the text contains \E: \E ends the quote early, and PCRE2 parses the remaining text as pattern syntax.
use "regex"
let exact = Regex(
RegexEscape.pattern(user_text)
where options = [RegexAnchored; RegexEndAnchored])?
let prefixed = Regex("\\A" + RegexEscape.pattern(prefix) + "\\d+\\z")?RegexEscape.pattern works with every RegexCompileOption and under (?x) and (?xx). It produces valid UTF-8 from valid UTF-8 input, keeps invalid UTF-8 invalid so Regex.create rejects the result, and never returns raw NUL bytes. Use RegexEscape.pattern for Regex patterns only, not for Regex.replace replacement strings.
Fix Match memory safety after disposal and garbage collection
Previously, the garbage collector could collect a Regex while a Match from it was still in use, causing reads of freed memory. A Match now keeps its Regex alive while the Match is in use.
Calling Match methods after Match.dispose() or Regex.dispose() no longer crashes or reads freed memory. size(), start_pos(), end_pos(), and end_pos_exclusive() keep their values, apply and find raise an error, and groups() returns an empty array.
Fix Match.find truncating capture names at NUL bytes
Previously, when a capture name contained a NUL byte (\0), Match.find truncated the name at the first NUL byte and looked up that prefix instead of raising an error. For example, for a match m of (?<foo>abc) on "abc", m.find("foo\0bar")? and m.find("foo\0")? returned "abc". Match.find now raises an error whenever the capture name contains a NUL byte.
Fix Match.apply and Match.find returning empty arrays for Array[U8] iso
Previously, Match.apply[Array[U8] iso] and Match.find[Array[U8] iso] returned an empty Array[U8] even when the group captured bytes. For example, for a match m of (?<word>hello) on "hello", m.apply[Array[U8] iso](0)? and m.find[Array[U8] iso]("word")? returned an empty array. Both methods now return the captured bytes.
[3.0.0] - 2026-12-03
Fixed
- Fix
matcheson empty matches, and match positions for patterns that use\K(PR #100) - Fix
Regex.indexfailing to link (PR #117) - Fix
Regex.splitholding match data until garbage collection (PR #118) - Fix
(*NO_JIT)matching, JIT UTF-8 and offset checks, and slowmatchesandsplit(PR #120) - Fix JIT stack limit failures on backtracking patterns (PR #121)
- Enable PCRE2 JIT support in Windows builds (PR #122)
- Make global
Regex.replaceconsistent across PCRE2 versions (PR #124) - Fix
Matchmemory safety after disposal and garbage collection (PR #123) - Fix
Match.findtruncating capture names at NUL bytes (PR #132) - Fix
Match.applyandMatch.findreturning empty arrays forArray[U8] iso(PR #133)
Added
- Add
Match.end_pos_exclusive()(PR #108) - Add compile options to
Regex.create(PR #110) - Add
RegexEscape.patternto escape literal text for patterns (PR #126)
Changed
- Reject
\Cin regular expression patterns (PR #119)