tags:

views:

30

answers:

2

Hi

I want to search for all possible dates in a string using Regex. In my code i have this:

String dateSearchPattern = @"(?<Day>\d{2}).(?<Month>\d{2}).(?<Year>\d{4})|(?<Day>\d{2}).(?<Month>\d{2}).(?<Year>\d{2})";

// date format: dd.mm.yyyy or d.m.yyyy or dd.mm.yy or d.m.yy
String searchText = "20.03.2010.25.03.10";

Regex.Matches(searchText, dateSearchPattern); // the matching SHOULD give a count of 2

The above code gives only 1 match where it should give 2. Also i need to have a patthern when the date format is like d.m.yyyy or d.m.yy.

A: 

The pattern seems perfectly ok. It is giving two match. By any chance have you used the following line to check the count?

var match = Regex.Matches(searchText, dateSearchPattern);
Console.WriteLine(match.Count);

I used SD 3 on .Net 3.5 (w/o sp1) and your code is giving your desired result.

Anindya Chatterjee
A: 

You can change your pattern to this:

"(?<Day>\d{1,2}).(?<Month>\d{1,2}).(?:(?<Year>\d{4})|(?<Year>\d{2}))"
George Howarth