问题
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.
回答1:
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);
}
回答2:
I think regular expressions are a bit of an overkill here.
NSString *words = @"nonsense non-issue anonymous controlWord";
NSArray *wordsArr = [words componentsSeparatedByString:@" "];
int count = 0;
for (NSString *word in wordsArr) {
if ([word hasPrefix:@"non"]) {
count++;
NSLog(@"%dth match: %@", count, word);
}
}
NSLog(@"Count: %d", count);
回答3:
There is more easier way to do this. You can use NSPredicate and use this format BEGINSWITH[c] %@.
Sample code
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"Firstname BEGINSWITH[c] %@", text];
NSArray *results = [People filteredArrayUsingPredicate:resultPredicate];
来源:https://stackoverflow.com/questions/14739627/how-to-use-regular-expressions-to-find-words-that-begin-with-a-three-character-p