Skip to content

Conversation

@PranavSenthilnathan
Copy link
Member

When the pattern expression is folded into a constant that needs escaping, we choose to use a verbatim string literal. However, verbatim strings also need to escape one character - the double quote.

The code generated for

var isMatch = Regex.IsMatch("", "[" + @"\/:<>|" + "\"]");

is now:

[GeneratedRegex(@"[\/:<>|""]")]
private static partial Regex MyRegex();

Fixes #104371

@PranavSenthilnathan PranavSenthilnathan added this to the 10.0.0 milestone May 11, 2025
@PranavSenthilnathan PranavSenthilnathan self-assigned this May 11, 2025
Copilot AI review requested due to automatic review settings May 11, 2025 04:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes the verbatim escaping of double quotes in the regex code generator.

  • Adds a new test to verify correct escaping when constant pattern expressions include quotes.
  • Updates the code fixer to replace double quotes with two double quotes for verbatim string literals.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs Added a new test case to validate escaping of quotes in regex patterns.
src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs Modified the code fixer to correctly escape double quotes in verbatim strings.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-regularexpressions
See info in area-owners.md if you want to be subscribed.

@PranavSenthilnathan PranavSenthilnathan merged commit 8b80947 into dotnet:main May 11, 2025
84 of 87 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jun 11, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SYSLIB1045 Quick fix generates badly formed string

3 participants