2

I am struggling to get my head around some regular expression to match some date formats. Ideally i would like one expression too match every date format possible, i am searching a email for all dates with in the body.

Formats such as:

Wednesday, 6 November 2013
Weds 6th November 2013
Weds 6th Nov 2013

06/11/2013
11/06/2013
06/11/13
11/06/13

6th Nov 2013
6th November 2013

Anyone know of an all in one expression I could use?

Jerry
  • 70,495
  • 13
  • 100
  • 144
Neil Faulkner
  • 526
  • 1
  • 4
  • 22
  • Possible duplicate: [regex for various date formats](http://stackoverflow.com/q/11516129/1578604). – Jerry Jul 06 '13 at 13:02

4 Answers4

5

Thanks to Wain's answer I'm using this code instead of NSRegularExpression to find dates within a string, hope it might help anyone with a similar problem.

NSError *error = NULL;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];

NSArray *matches = [detector matchesInString:string
                                     options:0
                                       range:NSMakeRange(0, [string length])];

for (NSTextCheckingResult *match in matches) {
    
    if ([match resultType] == NSTextCheckingTypeDate) {
        
        NSDate *date = [match date];
        
        NSDateFormatter *formatter;
        NSString        *dateString;
        
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
        
        dateString = [formatter stringFromDate:date];
        NSLog(@"Date: %@",dateString);
   
    }
}
Arasuvel
  • 2,971
  • 1
  • 25
  • 40
Neil Faulkner
  • 526
  • 1
  • 4
  • 22
3

Consider using NSDataDetector which can be configured to scan for dates (NSTextCheckingTypeDate). Docs here.

Wain
  • 118,658
  • 15
  • 128
  • 151
0

you can write regular expression with several strings
also you can test regular expression in http://regexpal.com its different since you remove \ in the expression for example (\d{2})([./-])(\d{2})([./-])(\d{2,4})

NSError *error = NULL;  
    NSString *expForSlash = @"(\\d{2})((\/)|(\.))(\\d{2})((\/)|(\.))(\\d{4}|\\d{2})";



NSString *expMonthStrNew = @"(Jan|Feb|Mar(ch)?|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(\\s?)(\\d{2}|\\d{1})(\,?)(\\s?)(\\d{4}|\\d{2})";
NSString *expForreg  = [NSString stringWithFormat:@"%@|%@", expForSlash, expMonthStrNew];//@"(May|Jun)(\\s?)(\\d{2})(\,?)(\\s?)(\\d{4})";

NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:expForreg
                              options:NSRegularExpressionCaseInsensitive
                              error:&error];



NSArray *matches = [regex matchesInString:tesText options:0 range:NSMakeRange(0, [tesText length])];
miv
  • 1
0

Improving the response of Neil Faulkner.

In case that you want to get the date with localization format:

NSError *error = nil;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeDate error:&error];

NSArray *matches = [detector matchesInString:value
                                     options:0
                                       range:NSMakeRange(0, [value length])];

for (NSTextCheckingResult *match in matches)
{

    if ([match resultType] == NSTextCheckingTypeDate)
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.locale = [NSLocale currentLocale];
        formatter.dateStyle = NSDateFormatterShortStyle;
        response = [formatter stringFromDate:[match date]];
    }
}
Mara Jimenez
  • 846
  • 8
  • 13