All rules
CA1874Performance Enabled by default: As suggestion
Use Regex.IsMatch
Use Regex.IsMatch
Microsoft docsDescription
System.Text.RegularExpressions.Regex.IsMatch is simpler and faster than Regex.Match(...).Success. The IsMatch method is optimized for the case where you only need to know whether a match exists, rather than what the match is. Calling Match() and then checking System.Text.RegularExpressions.Group.Success does unnecessary work that can impact performance.
Cause
The System.Text.RegularExpressions.Group.Success property of the result from System.Text.RegularExpressions.Regex.Match is used to check if a pattern matches.
How to fix violations
Replace calls to Regex.Match(...).Success with Regex.IsMatch(...).
A *code fix* that automatically performs this transformation is available.
Example
using System.Text.RegularExpressions;
class Example
{
public bool IsValidEmail(string email)
{
// Violation
return Regex.Match(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$").Success;
}
}
using System.Text.RegularExpressions;
class Example
{
public bool IsValidEmail(string email)
{
// Fixed
return Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$");
}
}When to suppress
It's safe to suppress a warning from this rule if performance isn't a concern.
Your vote
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0