Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use regular expressions to find words that begin with a three character prefix

My goal is to count the number of words (in a string) that begin with a specified prefix of more than one letter. A case is words that begin with "non". So in this example...

NSString * theFullTestString = @"nonsense non-issue anonymous controlWord";

...I want to get hits on "nonsense" and "non-issue", but not on "anonymous" or "controlWord". The total count of my hits should be 2.

So here's my test code which seems close, but none of the regular expression forms I've tried works correctly. This code catches "nonsense" (correct) and "anonymous" (wrong) but not "non-issue" (wrong). Its count is 2, but for the wrong reason.

NSUInteger countOfNons = 0;
NSString * theFullTestString = @"nonsense non-issue anonymous controlWord";
NSError *error = nil;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"non(\\w+)" options:0 error:&error];

NSArray *matches = [regex matchesInString:theFullTestString options:0 range:NSMakeRange(0, theFullTestString.length)];

for (NSTextCheckingResult *match in matches) {
    NSRange wordRange = [match rangeAtIndex:1];
    NSString* word = [theFullTestString substringWithRange:wordRange];
    ++countOfNons;
    NSLog(@"Found word:%@  countOfNons:%d", word, countOfNons);
}

I'm stumped.

like image 592
Han Avatar asked Feb 06 '13 21:02

Han


1 Answers

The regex \bnon[\w-]* should do the trick

\bnon[\w-]*
^ (\b) Start of word
  ^ (non) Begins with non
     ^ ([\w-]) A alphanumeric char, or hyphen
          ^ (*) The character after 'non' zero or more times

So, in your case:

NSUInteger countOfNons = 0;
NSString * theFullTestString = @"nonsense non-issue anonymous controlWord";
NSError *error = nil;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\bnon[\\w-]*)" options:0 error:&error];

NSArray *matches = [regex matchesInString:theFullTestString options:0 range:NSMakeRange(0, theFullTestString.length)];

for (NSTextCheckingResult *match in matches) {
    NSRange wordRange = [match rangeAtIndex:1];
    NSString* word = [theFullTestString substringWithRange:wordRange];
    ++countOfNons;
    NSLog(@"Found word:%@  countOfNons:%d", word, countOfNons);
}
like image 77
WDUK Avatar answered Sep 22 '22 11:09

WDUK