Vins
Vins

Reputation: 1934

Search tags in string

I have a string, and I want to search words (tags) that begin with "#" and end with "." or "," or " " I found this online, but is limited because: - You can find a single word in the string (although there are more words) - "RangeOfString" does not allow multiple choices

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];
    }
}

you have idea how can I do?

Thank you!

Upvotes: 1

Views: 603

Answers (3)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

You will most likely want to use 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);

At that point you just loop until [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);

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

You can use NSRegularExpression class, like this:

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]]);
}

You may need to play with your regular expression to get it just right. The reference that I liked describes the grammar of the regex language supported by Apple's classes.

Upvotes: 2

Peter DeWeese
Peter DeWeese

Reputation: 18333

You should use NSRegularExpression, which will give you multiple matches. The following is an untested example:

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])];

Upvotes: 1

Related Questions