diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs index 82698721b..660d09925 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs @@ -34,6 +34,11 @@ internal class DocumentationSettings /// internal static readonly ImmutableArray DefaultExcludeFromPunctuationCheck = ImmutableArray.Create("seealso"); + /// + /// This is a Regex instance matching valid variables. + /// + private static readonly Regex ValidVariableNameRegex = new Regex("^[a-zA-Z0-9]+$"); + /// /// This is the backing field for the property. /// @@ -199,7 +204,7 @@ protected internal DocumentationSettings(JsonObject documentationSettingsObject, { string name = child.Key; - if (!Regex.IsMatch(name, "^[a-zA-Z0-9]+$")) + if (!ValidVariableNameRegex.IsMatch(name)) { continue; } diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs index 9dd7b5e96..f956b5d9e 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs @@ -13,6 +13,8 @@ namespace StyleCop.Analyzers.Settings.ObjectModel internal class NamingSettings { + private static readonly Regex ValidHungarianPrefixRegex = new Regex("^[a-z]{1,2}$"); + /// /// Initializes a new instance of the class. /// @@ -55,7 +57,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi { var prefix = prefixJsonValue.ToStringValue(kvp.Key); - if (!Regex.IsMatch(prefix, "^[a-z]{1,2}$")) + if (!ValidHungarianPrefixRegex.IsMatch(prefix)) { continue; } @@ -86,7 +88,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi allowCommonHungarianPrefixes ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.naming.allowCommonHungarianPrefixes"); allowedHungarianPrefixes ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedHungarianPrefixes") - ?.Where(value => Regex.IsMatch(value, "^[a-z]{1,2}$")) + ?.Where(value => ValidHungarianPrefixRegex.IsMatch(value)) .ToImmutableArray() .ToBuilder(); allowedNamespaceComponents ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedNamespaceComponents")?.ToBuilder();