I have a regular expression which matches a date format like : 26 August 2011
and I’m trying to read each line in a file and capture the line that contains the date in above format. But it does not seem to be working:
Regex test = new Regex(@"^((31(?!\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\b|t)t?|Nov)(ember)?)))|((30|29)(?!\ Feb(ruary)?))|(29(?=\ Feb(ruary)?\ (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\b|t)t?|Nov|Dec)(ember)?)\ ((1[6-9]|[2-9]\d)\d{2})$");
StreamReader file = new StreamReader(outputFile);
while ((line2 = file.ReadLine()) != null)
{
lines.Add(line2);
foreach (Match match in test.Matches(line2))
{
v += match.Value;
}
}
Ok, so this is the scenario..
1st – If line contains: “26 August 2011”, it returns that date.
2nd – If line contains : ” some text etc 26 August 2011″, it returns null.
Any idea how this issue can be tackled?
The leading
^character in your regular expression says, “match starting at the beginning of the line.” And the last character is$, meaning that the line has to end with the expression. So if your line contains anything other than a date in the format you specified, the regular expression isn’t going to match.Remove the
^at the front and the$at the end.