在字符串中搜索标签

时间:2012-04-27 15:06:16

标签: objective-c xcode tags nsstring

我有一个字符串,我想搜索以“#”开头并以“。”结尾的单词(标签)。或“,”或“”我在网上找到了这个,但是因为: - 你可以在字符串中找到一个单词(尽管有更多的单词) - “RangeOfString”不允许多项选择

NSString *stringText = @"test #hello #world";
NSString *result = nil;

// Determine  "#"
NSRange hashRange = [stringText rangeOfString:@"#" options:NSCaseInsensitiveSearch];
if (hashRange.location != NSNotFound)
{
    // Determine " " location according to "#" location
    NSRange endHashRange;

    endHashRange.location = hashRange.length + hashRange.location;
    endHashRange.length   = [stringText length] - endHashRange.location;
    endHashRange = [stringText rangeOfString:@" " options:NSCaseInsensitiveSearch range:endHashRange];

    if (endHashRange.location != NSNotFound)
    {
        // Tags found: retrieve string between them
        hashRange.location += hashRange.length;
        hashRange.length = endHashRange.location - hashRange.location;

        result = [stringText substringWithRange:hashRange];
    }
}

你知道我该怎么办?

谢谢!

3 个答案:

答案 0 :(得分:2)

您可以使用NSRegularExpression类,如下所示:

NSError *error = NULL;
NSRegularExpression *tags = [NSRegularExpression
    regularExpressionWithPattern:@"[#]([^, .]+)([, .]|$)"
    options:NSRegularExpressionCaseInsensitive
    error:&error];
NSArray *matches = [tags matchesInString:str options:0 range:NSMakeRange(0, str.length)];

for (NSTextCheckingResult *match in matches) {
    NSLog(@"%@", [str substringWithRange:[match rangeAtIndex:1]]);
}

您可能需要使用正则表达式才能正确使用它。我喜欢的引用描述了Apple类支持的正则表达式语法。

答案 1 :(得分:1)

您应该使用NSRegularExpression,这会为您提供多个匹配项。 以下是未经测试的示例:

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b(#\\S*[.,])\\b" options:NSRegularExpressionCaseInsensitive];
int numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, string.length)];
NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];

答案 2 :(得分:1)

您很可能想要使用NSScanner。

NSString *stringText = @"test #hello #world";
NSString *result = nil;
NSCharacterSet *endingChars = [NSCharacterSet characterSetWithCharactersInString:@"., "];

NSScanner *scanner = [NSScanner scannerWithString:stringText];
scanner.charactersToBeSkipped = nil;

[scanner scanUpToString:@"#" intoString:NULL];
[scanner scanString:@"#" intoString:NULL];
[scanner scanUpToCharactersFromSet:endingChars intoString:&result];
[scanner scanCharactersFromSet:endingChars intoString:NULL];

STAssertEqualObjects(result, @"hello", nil);

此时你只需循环直到[scanner isAtEnd];


NSString *stringText = @"test #hello #world";
NSString *match = nil;
NSMutableArray *results = [NSMutableArray arrayWithCapacity:2];
NSCharacterSet *endingChars = [NSCharacterSet characterSetWithCharactersInString:@"., "];

NSScanner *scanner = [NSScanner scannerWithString:stringText];
scanner.charactersToBeSkipped = nil;

while (![scanner isAtEnd]) {
    [scanner scanUpToString:@"#" intoString:NULL];
    [scanner scanString:@"#" intoString:NULL];
    [scanner scanUpToCharactersFromSet:endingChars intoString:&match];
    [scanner scanCharactersFromSet:endingChars intoString:NULL];
    [results addObject:match];
}

STAssertEquals(results.count, 2, nil);
STAssertEqualObjects([results objectAtIndex:0], @"hello", nil);
STAssertEqualObjects([results objectAtIndex:1], @"world", nil);