Repository: dotnet/runtime
File: src/libraries/Common/src/System/Data/Common/DbConnectionOptions.Common.cs (C#)

Code before:
                + "([^;\\s\\p{Cc}]|\\s+[^;\\s\\p{Cc}])*"                    // control characters must be quoted
                + "(?<![\"']))"                                            // unquoted value must not stop with " or '
                + ")(\\s*)(;|[\u0000\\s]*$)"                               // whitespace after value up to semicolon or end-of-line
                + ")*"                                                     // repeat the key-value pair
                + "[\\s;]*[\u0000\\s]*"                                    // trailing whitespace/semicolons (DataSourceLocator), embedded nulls are allowed only in the end
            ;

        private const string ConnectionStringPatternOdbc =                 // may not contain embedded null except trailing last value
                "([\\s;]*"                                                 // leading whitespace and extra semicolons
                + "(?![\\s;])"                                             // key does not start with space or semicolon
                + "(?<key>([^=\\s\\p{Cc}]|\\s+[^=\\s\\p{Cc}])+)"           // allow any visible character for keyname except '='
                + "\\s*=\\s*"                                              // the equal sign divides the key and value parts
                + "(?<value>"
                + "(\\{([^\\}\u0000]|\\}\\})*\\})"                         // quoted string, starts with { and ends with }
                + "|"
                + "((?![\\{\\s])"                                          // unquoted value must not start with { or space, would also like = but too late to change
                + "([^;\\s\\p{Cc}]|\\s+[^;\\s\\p{Cc}])*"                   // control characters must be quoted

                + ")" // although the spec does not allow {} embedded within a value, the retail code does.
                + ")(\\s*)(;|[\u0000\\s]*$)"                               // whitespace after value up to semicolon or end-of-line
                + ")*"                                                     // repeat the key-value pair
                + "[\\s;]*[\u0000\\s]*"                                    // trailing whitespace/semicolons (DataSourceLocator), embedded nulls are allowed only in the end
            ;

        private static readonly Regex s_connectionStringRegex = CreateConnectionStringRegex;
        private static readonly Regex s_connectionStringRegexOdbc = CreateConnectionStringRegexOdbc;

#if NET
        [GeneratedRegex(ConnectionStringPattern, RegexOptions.ExplicitCapture)]
        private static partial Regex CreateConnectionStringRegex { get; }

        [GeneratedRegex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture)]
        private static partial Regex CreateConnectionStringRegexOdbc { get; }
#else
        private static Regex CreateConnectionStringRegex() => new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
        private static Regex CreateConnectionStringRegexOdbc() => new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif
#endif
        internal const string DataDirectory = "|datadirectory|";

        private static readonly Regex s_connectionStringValidKeyRegex = CreateConnectionStringValidKeyRegex; // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
        private static readonly Regex s_connectionStringQuoteValueRegex = CreateConnectionStringQuoteValueRegex; // generally do not quote the value if it matches the pattern
        private static readonly Regex s_connectionStringQuoteOdbcValueRegex = CreateConnectionStringQuoteOdbcValueRegex; // do not quote odbc value if it matches this pattern

#if NET
        [GeneratedRegex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$")]
        private static partial Regex CreateConnectionStringValidKeyRegex { get; }
        [GeneratedRegex("^[^\"'=;\\s\\p{Cc}]*$")]
        private static partial Regex CreateConnectionStringQuoteValueRegex { get; }
        [GeneratedRegex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture)]
        private static partial Regex CreateConnectionStringQuoteOdbcValueRegex { get; }

Review comment (suggestion):
@copilot, these fields should be deleted. Instead, the #else blocks below should be changed to use properties instead of methods, andthen all call sites should be changed to reference tose properties rather than referencing the fields.

Code after:
                + "([^;\\s\\p{Cc}]|\\s+[^;\\s\\p{Cc}])*"                    // control characters must be quoted
                + "(?<![\"']))"                                            // unquoted value must not stop with " or '
                + ")(\\s*)(;|[\u0000\\s]*$)"                               // whitespace after value up to semicolon or end-of-line
                + ")*"                                                     // repeat the key-value pair
                + "[\\s;]*[\u0000\\s]*"                                    // trailing whitespace/semicolons (DataSourceLocator), embedded nulls are allowed only in the end
            ;

        private const string ConnectionStringPatternOdbc =                 // may not contain embedded null except trailing last value
                "([\\s;]*"                                                 // leading whitespace and extra semicolons
                + "(?![\\s;])"                                             // key does not start with space or semicolon
                + "(?<key>([^=\\s\\p{Cc}]|\\s+[^=\\s\\p{Cc}])+)"           // allow any visible character for keyname except '='
                + "\\s*=\\s*"                                              // the equal sign divides the key and value parts
                + "(?<value>"
                + "(\\{([^\\}\u0000]|\\}\\})*\\})"                         // quoted string, starts with { and ends with }
                + "|"
                + "((?![\\{\\s])"                                          // unquoted value must not start with { or space, would also like = but too late to change
                + "([^;\\s\\p{Cc}]|\\s+[^;\\s\\p{Cc}])*"                   // control characters must be quoted

                + ")" // although the spec does not allow {} embedded within a value, the retail code does.
                + ")(\\s*)(;|[\u0000\\s]*$)"                               // whitespace after value up to semicolon or end-of-line
                + ")*"                                                     // repeat the key-value pair
                + "[\\s;]*[\u0000\\s]*"                                    // trailing whitespace/semicolons (DataSourceLocator), embedded nulls are allowed only in the end
            ;

#if NET
        [GeneratedRegex(ConnectionStringPattern, RegexOptions.ExplicitCapture)]
        private static partial Regex ConnectionStringRegex { get; }

        [GeneratedRegex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture)]
        private static partial Regex ConnectionStringRegexOdbc { get; }
#else
        private static Regex ConnectionStringRegex { get; } = new Regex(ConnectionStringPattern, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
        private static Regex ConnectionStringRegexOdbc { get; } = new Regex(ConnectionStringPatternOdbc, RegexOptions.ExplicitCapture | RegexOptions.Compiled);
#endif
#endif
        internal const string DataDirectory = "|datadirectory|";

#if NET
        [GeneratedRegex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$")]
        private static partial Regex ConnectionStringValidKeyRegex { get; } // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
        [GeneratedRegex("^[^\"'=;\\s\\p{Cc}]*$")]
        private static partial Regex ConnectionStringQuoteValueRegex { get; } // generally do not quote the value if it matches the pattern
        [GeneratedRegex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture)]
        private static partial Regex ConnectionStringQuoteOdbcValueRegex { get; } // do not quote odbc value if it matches this pattern
#else
        private static Regex ConnectionStringValidKeyRegex { get; } = new Regex("^(?![;\\s])[^\\p{Cc}]+(?<!\\s)$", RegexOptions.Compiled); // key not allowed to start with semi-colon or space or contain non-visible characters or end with space
        private static Regex ConnectionStringQuoteValueRegex { get; } = new Regex("^[^\"'=;\\s\\p{Cc}]*$", RegexOptions.Compiled); // generally do not quote the value if it matches the pattern
        private static Regex ConnectionStringQuoteOdbcValueRegex { get; } = new Regex("^\\{([^\\}\u0000]|\\}\\})*\\}$", RegexOptions.ExplicitCapture | RegexOptions.Compiled); // do not quote odbc value if it matches this pattern
#endif

        // connection string common keywords