忽略以标签或提及开头的单词

时间:2014-09-14 04:10:21

标签: ios objective-c nsregularexpression

请原谅我的noobness,但我无法构建一个模式来忽略以#标签或提及符号@开头的单词。我知道要选择以hashtags开头的单词,我可以做#(\ w +)并选择以提及开头的单词@(\ w +)。

我尝试做这样的事情(?!@(\ w +))来忽略提及和这个(?!#(\ w +))忽略主题标签但没有运气。我将继续努力,寻找任何帮助表示赞赏。

我想创建一个模式:!(hashtags)AND!(提及) 或者只选择单词的模式。

2 个答案:

答案 0 :(得分:1)

试试这个:

NSMutableArray *words = [[text componentsSeparatedByString:@" "] mutableCopy];
for (NSString *word in words)
{
    NSString *firstCharacter = [word substringToIndex:1];
    if ([firstCharacter isEqual:@"#"] || [firstCharacter isEqual:@"@"])
    {
        // remove the word from the array here or whatever
        [words removeObject:word];
    }
}

答案 1 :(得分:0)

以下是根据freshking提供的答案为我工作的解决方案

- (NSMutableArray *) checkForWords:(UITextField *)textField {
    NSMutableArray *words = [[NSMutableArray alloc] initWithArray:[[textField.text componentsSeparatedByString:@" "] mutableCopy]];
    NSMutableArray *matches = [[NSMutableArray alloc] init];
    //NSLog(@"words: %@",words);

    for (NSString *word in words) {
        NSString *firstCharacter = [word substringToIndex:1];
        if (![firstCharacter isEqual:@"#"] && ![firstCharacter isEqual:@"@"]) {
            [matches addObject:word];
        }
    }

    return matches;
}
相关问题