Skip to content

Fix Regex.Replace to replace all matches - #4040

Merged
roji merged 1 commit into
mainfrom
roji-regex-replace-triage
Oct 19, 2026
Merged

roji merged 1 commit into
mainfrom
roji-regex-replace-triage

Conversation

@roji

@roji roji commented Oct 19, 2026

Copy link
Copy Markdown
Member

.NET's Regex.Replace replaces all matches, but its PostgreSQL translation omitted the g flag and replaced only the first match, producing incorrect query results.

Always append g in TranslateReplace, including when the other options produce no flags. Shared option translation and Regex.Count are unchanged. Strengthen the existing replacement tests with multi-match patterns and update their SQL baselines.

Fixes #4035

Always include the global flag in regexp_replace and exercise multiple matches in existing replacement tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4633d5dd-172b-4c7a-ba3d-c19e02859860
Copilot AI balanced review requested due to automatic review settings October 19, 2026 09:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix covers both translated overloads and empty option flags with regression coverage; tests were not executed.

No open issues 🎉

What changed in this PR

Fixes #4035 by making PostgreSQL translation of Regex.Replace replace all matches, matching .NET behavior.

Changes:

  • Always appends g to replacement flags, including when other flags are empty.
  • Strengthens regression tests with multi-match patterns and updated SQL expectations.
  • Leaves shared option translation and Regex.Count unchanged.
File Description
test/​EFCore.PG.FunctionalTests/​Query/​NorthwindFunctionsQueryNpgsqlTest.cs Tests multi-match replacements across supported options.
src/​EFCore.PG/​Query/​ExpressionTranslators/​Internal/​NpgsqlRegexTranslator.cs Always supplies the global replacement flag.

Review effort: Balanced

@roji
roji merged commit f77bb40 into main Oct 19, 2026
16 checks passed
@roji
roji deleted the roji-regex-replace-triage branch October 19, 2026 09:47
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.

Regex.Replace is translated to regexp_replace without the g flag — only the first match is replaced

2 participants