0

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.

poundifdef
  • 18,726
  • 23
  • 95
  • 134
Ron Myschuk
  • 6,011
  • 2
  • 20
  • 32
  • Have you checked out the answer here: http://stackoverflow.com/questions/1752946/how-to-get-the-first-n-words-from-a-nsstring-in-objective-c – Bruce Dean Nov 16 '13 at 17:44
  • that only divides the text into words and returns x number of words, I've already got that functionality. – Ron Myschuk Nov 16 '13 at 18:00
  • I'm unclear on what you're trying to achieve. What are you searching through in relation to the phrase `I haven't has a chance to introduce my self...`, that you're finding `I hate snow badly`. Does not seem like a problem for which regex is the solution. – OGHaza Nov 16 '13 at 18:10
  • many of the returned results will be unrelated, the only relation in the return results is that they start with the same character. I am not sure if regex is the answer either. Ultimately I would have like to have used something like this... if ([someString hasPrefix:result]) but "I have" returns true on that. – Ron Myschuk Nov 16 '13 at 18:17
  • have you thought of using NSPredicate? – Ilario Nov 16 '13 at 19:25
  • I have... I know that NSPredicate has the BeginsWith option, but I fail to see how that will be any different than hasPrefix, there was an answer here a few minutes ago (but it has disappeared) about using hasPrefix, but it doesn't work, because it doesn't match based on the whole word. – Ron Myschuk Nov 16 '13 at 19:32

1 Answers1

0

I just got linked here from a more recent question so I tried an expression that might be useful:

"I\\shaven't.*\\."

Basically it finds "I haven't" and matches everything till the end of that sentence. Might be useful for anyone facing similar problems.

Ben Ong
  • 913
  • 1
  • 10
  • 25