Expressoes Regulares no iPhone

Posted on segunda-feira, junho 22, 2009 -

Bem util ! Seguem exemplos pra quem não vive sem:

- (BOOL)assertRegex:(NSString*)stringToSearch withRegex:(NSString*)regexString {
NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexString];
return [regex evaluateWithObject:stringToSearch];
}

- (void)testRegexes {
// Find a string at the end of a line.
STAssertTrue([self assertRegex:@"Christopher Bartling"
withRegex:@".*Bartling$"], @"Regex did not match.");

// Find a string at the beginning of a line.
STAssertTrue([self assertRegex:@"Christopher Bartling"
withRegex:@"^Chris.*"], @"Regex did not match.");

// Find a sequence of 3 characters in succession using an interval quantifier (e.g {3,3}).
STAssertTrue([self assertRegex:@"**** = *** === ****** == *****"
withRegex:@".*={3,3}.*"], @"Regex did not match.");
}

A quem mais interessar também existem algumas frameworks para isso:
http://regexkit.sourceforge.net/
http://sourceforge.net/projects/objpcre

Recent Posts

Followers