1

我有一个字符串,我想搜索以“#”开头并以“.”结尾的单词(标签)。或“,”或“”我在网上找到的,但受到限制,因为: - 您可以在字符串中找到单个单词(尽管有更多单词) - “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];
    }
}

你知道我该怎么做吗?

谢谢!

4

3 回答 3

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 类支持的正则表达式语言的语法。

于 2012-04-27T15:20:37.767 回答
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])];
于 2012-04-27T15:19:52.853 回答
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);
于 2012-04-27T15:27:14.933 回答