I need to perform search where the beginning a string get's matched on whole words, because of the whole word option I was thinking Regex???
these is the methods I am trying to use but admit-tingly do not know much about regex in obj-c
- (BOOL)searchText:(NSString *)searchString inString:(NSString *)text {
NSRegularExpression *regex = [self regularExpressionWithString:searchString];
NSArray *found = [regex matchesInString:text options:NSRegularExpressionCaseInsensitive|NSRegularExpressionAnchorsMatchLines range:NSMakeRange(0, text.length)];
if (found.count > 0)
return YES;
else
return NO;
}
- (NSRegularExpression *)regularExpressionWithString:(NSString *)string {
NSError *error = NULL;
NSString *pattern = [NSString stringWithFormat:@"\\b%@\\b", string];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
if (error)
NSLog(@"Couldn't create regex with given string and options");
return regex;
}
The searched string will be long (250 characters) and I have it broken up into words, then I am looking for matching phrases at the front of the string, if no matches I take away the first word and start over.
if the searched string is "I haven't has a chance to introduce my self..."
search through for phrases that start with "i" and it might find these results in order of longest length...
I hate snow badly
I swim daily
I have
is
those results should all fail, but right now it is matching on "I have", I need a search that would only match if "I haven't" was in the returned results not I have, and then I would move not to the next word and search for phrases starting with "haven't"
To further complicate things I need to ignore punctuation around words.