[英]Not sure why I can't match this regex in C#
I'm trying to match the string "September 12" with the following C# code. 我正在尝试将字符串“9月12日”与以下C#代码匹配。 But it won't match and I'm not sure why.
但它不匹配,我不知道为什么。 What am I doing wrong?
我究竟做错了什么? It appears to work on regexpal.com
它似乎适用于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. 问题是标志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".
删除它,因为你不想忽略正则表达式中的空格 - 你需要它来匹配“九月”和“19”之间的空格。
Hint: digits
can be written more easy as [0-9]. 提示:
digits
可以更容易写为[0-9]。 A better regular expression would be 一个更好的正则表达式将是
September [0-9]+
As @Moritz pointed out your are not matching because you are Ignoring Whitespace. 正如@Moritz指出你不匹配因为你忽略了空白。 You should also note that your current method will match a wide range of "dates" that are invalid.
您还应该注意,您当前的方法将匹配无效的各种“日期”。
September 67
for instance. 例如,
September 67
。
I would recommend using a slightly more complex pattern for matching the number pattern: 我建议使用稍微复杂的模式来匹配数字模式:
September ([1-9]|[12][0-9]|3[01])
This will limit the numbers to between 1 and 31. While this will still allow some invalid dates ( September 31
for instance) it will greatly limit the number of invalid dates matched. 这将数字限制在1到31之间。虽然这仍然允许一些无效日期(例如
September 31
),但它将极大地限制匹配的无效日期的数量。
@"September\\s\\d+" should do it @“九月\\ s \\ d +”应该这样做
The \\s matches a space, the \\d matches any digit, and the + is 1 or more of the preceding. \\ s匹配一个空格,\\ d匹配任何数字,+是前面的1或更多。
You could try it this way. 你可以这样试试。
public static void Scan(String str)
{
// This regex is pretty nasty, I would probably take more time to refine it.
String patt = @"^([A-Za-z]+)(\s)(\d+)$";
foreach (Match match in Regex.Matches(str, patt, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace))
{
String value = match.Value;
Console.WriteLine(value);
}
}
... ...
Then call it like: 然后称之为:
Scan("September 2011");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.