I’m trying to match the string “September 12” with the following C# code. But it won’t match and I’m not sure why. What am I doing wrong? It appears to work on regexpal.com
public static void Scan(String str)
{
String digits = "(0|1|2|3|4|5|6|7|8|9)";
String r1 = "September " + digits + "+";
foreach (Match match in Regex.Matches(str, r1, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace))
{
String value = match.Value;
}
}
The problem is the flag RegexOptions.IgnorePatternWhitespace. Remove it since you don’t want to ignore whitespace in the regular expression – you need it to match the whitespace between “September” and “19”.
Hint:
digitscan be written more easy as [0-9]. A better regular expression would be