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

≯℡__Kan透↙ 提交于 2019-12-01 02:58:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!