diff --git a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs index d906d897db3afb..3075ea0811f5e5 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/UpgradeToGeneratedRegexCodeFixer.cs @@ -382,9 +382,9 @@ private static RegexOptions GetRegexOptionsFromArgument(ImmutableArray= 0; + } + private static string Literal(string stringifiedRegexOptions) { if (int.TryParse(stringifiedRegexOptions, NumberStyles.Integer, CultureInfo.InvariantCulture, out int options)) diff --git a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs index 4edea0da7c1280..91ebb77f9bc518 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs +++ b/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/UpgradeToGeneratedRegexAnalyzerTests.cs @@ -1339,6 +1339,70 @@ static void Main(string[] args) await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode); } + [Fact] + public async Task MultilineVerbatimStringPreservedByFixer() + { + string test = """ + using System.Text.RegularExpressions; + + static class Class + { + private static Regex r = [|new Regex(@"a + b + c", RegexOptions.IgnorePatternWhitespace)|]; + } + """; + + string expectedFixedCode = """ + using System.Text.RegularExpressions; + + static partial class Class + { + private static Regex r = MyRegex(); + + [GeneratedRegex(@"a + b + c", RegexOptions.IgnorePatternWhitespace)] + private static partial Regex MyRegex(); + } + """; + + await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode); + } + + [Fact] + public async Task MultilineStringConcatenationPreservedByFixer() + { + string test = """ + using System.Text.RegularExpressions; + + static class Class + { + private const string foo = "bar"; + private static Regex r1 = [|new Regex(@"a " + foo + @" + b + c", RegexOptions.IgnorePatternWhitespace)|]; + } + """; + + string expectedFixedCode = """ + using System.Text.RegularExpressions; + + static partial class Class + { + private const string foo = "bar"; + private static Regex r1 = MyRegex(); + + [GeneratedRegex(@"a bar + b + c", RegexOptions.IgnorePatternWhitespace)] + private static partial Regex MyRegex(); + } + """; + + await VerifyCS.VerifyCodeFixAsync(test, expectedFixedCode); + } + [Fact] public async Task TestAsArgument() {