Skip to content

fix(js/ts/python/beam): make a failed Regex.Match behave like Match.Empty - #5321

Merged
MangelMaxime merged 1 commit into
mainfrom
fix/regex-failed-match
Nov 20, 2026
Merged

MangelMaxime merged 1 commit into
mainfrom
fix/regex-failed-match

Conversation

@MangelMaxime

Copy link
Copy Markdown
Member

On .NET, a failed Regex.Match returns Match.Empty, an object with one failed group. Before this change, a failed match was null on JS/TS, None on Python, and a map with index => -1 on Beam.

In the table, m is Regex("(a)(?<name>b)").Match("xyz") and ok is Regex("(a)(b)?").Match("xa"). "ok" means that the target already gave the .NET value.

Expression .NET JS/TS before Python before Beam before
isNull m false true true ok
m.ToString() "" "null" "None" the printed map
m.Index 0 ok AttributeError -1
m.Value "" ok TypeError ok
m.Groups.Count 1 ok 0 3
m.Groups.[1].Success false ok IndexError ok
m.Groups.["name"].Success false ok ok badarith
isNull (m.NextMatch()) false true true compile error
ok.ToString() "a" "a,a," "<re.Match object ...>" the printed map
ok.Groups.[2].Length 0 ok TypeError ok
ok.Groups.[2].Index 0 compile error compile error -1
ok.Groups.[5].Success false ok IndexError function_clause

With this change, each cell gives the .NET value. The compile errors do not change.

Warning

On JS/TS and Python, a failed match is not null now. Code such as isNull m or Option.ofObj m now uses the non-null branch, as on .NET. To find a failed match, use m.Success.

Changes:

  • JS/TS: a failed match is one shared empty match. A Match used as a Group (:> Group, Seq.cast<Group>) now gives the correct Success, Value and Length.
  • Python: new EMPTY_MATCH and new library functions. The existing functions do not change. A Group stored in a variable reads about 1.1 times slower than on main. Direct m.Groups.[i] reads are faster.
  • Beam: a failed match and a failed group have index 0. A group index out of range or an unknown name gives a failed group.
  • Dart and Rust already return an object for a failed match. This PR does not change them.

Not fixed:

  • Beam: Match.NextMatch does not compile, and Groups.Count throws function_clause for a pattern with a lookbehind (?<=.
  • JS/TS, Python and Dart: Group.Index does not compile.
  • $"{m}", %A and a generic string x of a successful match do not give "a". Main has the same problem.
  • Dart: ToString() of a failed match and Groups.[5] do not give the .NET value. NextMatch and the Group upcast do not compile.
  • Rust: isNull m, m.ToString() and NextMatch do not compile.

@github-actions

Copy link
Copy Markdown
Contributor

Python Type Checking Results (Pyright)

Metric Value
Total errors 33
Files with errors 4
Excluded files 4
New errors ✅ No
Excluded files with errors (4 files)

These files have known type errors and are excluded from CI. Remove from pyrightconfig.ci.json as errors are fixed.

File Errors Status
temp/tests/Python/test_hash_set.py 18 Excluded
temp/tests/Python/test_applicative.py 11 Excluded
temp/tests/Python/test_nested_and_recursive_pattern.py 2 Excluded
temp/tests/Python/fable_modules/thoth_json_python/encode.py 2 Excluded

@MangelMaxime
MangelMaxime marked this pull request as ready for review November 20, 2026 14:54
@MangelMaxime
MangelMaxime merged commit 1644eeb into main Nov 20, 2026
44 checks passed
@MangelMaxime
MangelMaxime deleted the fix/regex-failed-match branch November 20, 2026 14:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant