From cd52915f17c5e59b1327e1dfbd08a9486c4e59ca Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 28 Sep 2026 16:24:59 +0200 Subject: [PATCH 1/4] Always add parentheses around regex pattern --- src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs index 9a82c8eed..e34e9271e 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs @@ -1011,9 +1011,9 @@ protected virtual Expression VisitRegexMatch(PgRegexMatchExpression expression, if (constantPattern is null) { - Sql.Append("' || "); + Sql.Append("' || ("); Visit(expression.Pattern); - Sql.Append(")"); + Sql.Append("))"); } else { From c32200c0cfb4cd636b74a9c3ec96a4da6ca72b77 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 28 Sep 2026 16:24:53 +0200 Subject: [PATCH 2/4] Update tests for changed regex generation --- .../Query/NorthwindFunctionsQueryNpgsqlTest.cs | 2 +- .../Query/Translations/StringTranslationsNpgsqlTest.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs index fe360fda2..29270abf0 100644 --- a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs @@ -101,7 +101,7 @@ await AssertQuery( SELECT c."CustomerID", c."Address", c."City", c."CompanyName", c."ContactName", c."ContactTitle", c."Country", c."Fax", c."Phone", c."PostalCode", c."Region" FROM "Customers" AS c -WHERE c."CompanyName" ~ ('(?p)' || @pattern) +WHERE c."CompanyName" ~ ('(?p)' || (@pattern)) """); } diff --git a/test/EFCore.PG.FunctionalTests/Query/Translations/StringTranslationsNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/Translations/StringTranslationsNpgsqlTest.cs index b72d06901..46ed613eb 100644 --- a/test/EFCore.PG.FunctionalTests/Query/Translations/StringTranslationsNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/Translations/StringTranslationsNpgsqlTest.cs @@ -1579,7 +1579,7 @@ public override async Task Regex_IsMatch_constant_input() """ SELECT b."Id", b."Bool", b."Byte", b."ByteArray", b."DateOnly", b."DateTime", b."DateTimeOffset", b."Decimal", b."Double", b."Enum", b."FlagsEnum", b."Float", b."Guid", b."Int", b."Long", b."Short", b."String", b."TimeOnly", b."TimeSpan" FROM "BasicTypesEntities" AS b -WHERE 'Seattle' ~ ('(?p)' || b."String") +WHERE 'Seattle' ~ ('(?p)' || (b."String")) """); } From b708a18b9042c4b1e80ae015d7b3ae8f22216447 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 28 Sep 2026 16:46:23 +0200 Subject: [PATCH 3/4] Also add parentheses in single-line case --- src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs index e34e9271e..6ae55abb1 100644 --- a/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs +++ b/src/EFCore.PG/Query/Internal/NpgsqlQuerySqlGenerator.cs @@ -979,7 +979,9 @@ protected virtual Expression VisitRegexMatch(PgRegexMatchExpression expression, // PG regexps are single-line by default if (options == RegexOptions.Singleline) { + Sql.Append("("); Visit(expression.Pattern); + Sql.Append(")"); return expression; } From 265879abb228130e90a88048a44c7c825485f21a Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 29 Sep 2026 09:08:57 +0200 Subject: [PATCH 4/4] Fix singleline tests --- .../Query/NorthwindFunctionsQueryNpgsqlTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs index 29270abf0..2def1992f 100644 --- a/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs +++ b/test/EFCore.PG.FunctionalTests/Query/NorthwindFunctionsQueryNpgsqlTest.cs @@ -197,7 +197,7 @@ await AssertQuery( """ SELECT c."CustomerID", c."Address", c."City", c."CompanyName", c."ContactName", c."ContactTitle", c."Country", c."Fax", c."Phone", c."PostalCode", c."Region" FROM "Customers" AS c -WHERE c."CompanyName" ~ '^A' +WHERE c."CompanyName" ~ ('^A') """); } @@ -213,7 +213,7 @@ await AssertQuery( """ SELECT c."CustomerID", c."Address", c."City", c."CompanyName", c."ContactName", c."ContactTitle", c."Country", c."Fax", c."Phone", c."PostalCode", c."Region" FROM "Customers" AS c -WHERE c."CompanyName" ~* '^a' +WHERE c."CompanyName" ~* ('^a') """); }