Ron
Ron

Reputation: 1047

NSPredicate not working

I have this string: <td align="right"><span>&nbsp;19:45&nbsp;</span></td>

I want to use a NSPredicate on it to search for the 19:45 part but every possible combination I tried returns nothing! I'm kinda losing my marbles here so please help!

Things i've tried:

NSString *timeStringPredicate = @"[0-9]:[0-9]";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:@"SELF like %@", timeStringPredicate];

if ([timeSearch evaluateWithObject:dayText]) {
NSLog(@"This is a time");
}

Or in these possibilities:

NSString *timeStringPredicate = @"[0-9]\\:[0-9]";
NSString *timeStringPredicate = @"*[0-9]:[0-9]*";
NSString *timeStringPredicate = @"*[0-9]\\:[0-9]*";
NSString *timeStringPredicate = @"*.[0-9]:[0-9].*";
NSString *timeStringPredicate = @"*.[0-9]\\:[0-9].*";

And about everything else.

Help!

Upvotes: 1

Views: 455

Answers (2)

YDT
YDT

Reputation: 11

Try to do this:

NSString *timeStringPredicate = @".*\\:[0-9].*";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:@"SELF matches '%@'", timeStringPredicate];

Upvotes: 0

Jim
Jim

Reputation: 73936

like doesn't use regexp syntax. For that, you need to use matches instead. See The Predicate Programming Guide for details.

NSString *timeStringPredicate = @".*\\:[0-9].*";

NSPredicate *timeSearch = [NSPredicate predicateWithFormat:@"SELF matches %@", timeStringPredicate];

Upvotes: 1

Related Questions