Here is a solution that does not make use of regex.
private static bool IsNumberOrGivenString(string number, string text, CultureInfo culture)
{
double result;
if (double.TryParse(number, NumberStyles.Float, culture, out result))
{
return true;
}
return number.Equals(text, StringComparison.OrdinalIgnoreCase);
}
private static bool IsNumberOrGivenString(string number, string text)
{
return IsNumberOrGivenString(number, text, CultureInfo.InvariantCulture);
}
Sample use:
Console.WriteLine(IsNumberOrGivenString("898", "all")); // true
Console.WriteLine(IsNumberOrGivenString("all", "all")); // true
Console.WriteLine(IsNumberOrGivenString("whatever", "all")); // false
Console.WriteLine(IsNumberOrGivenString("898,0", "all", CultureInfo.GetCultureInfo("sv-SE"))); // true
Console.WriteLine(IsNumberOrGivenString("898,0", "all", CultureInfo.GetCultureInfo("en-US"))); // false
The upsides with this code over using a regex is that it (may) run in a localized manner, using whatever decimal sign that is used. It will also fail the number if it has, say a .
in it, when that character is not a valid decimal separator.
Since the string comparison is ignoring case it will also match the word "all" regardless of whether it is "all", "All", "aLl" or any other combination of upper- and lowercase letters.