Quetool Quetool - 6 months ago 42
Objective-C Question

Regular expression to grub usernames from string

i need to find usernames (like twitter ones) in strings, for example, if the string is:

"Hello, @username! How are you? And @username2??"


I want to isolate/extract
@username
and
@username2


Do you know how to do it in Objective-C, i found this for Python regex for Twitter username but does not work for me

I tried it like this, but is not working:

NSString *comment = @"Hello, @username! How are you? And @username2??";

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=^|(?<=[^a-zA-Z0-9-\\.]))@([A-Za-z]+[A-Za-z0-9-]+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:comment options:0 range:NSMakeRange(0, comment.length)];
for (NSTextCheckingResult *match in matches) {
NSRange wordRange = [match rangeAtIndex:1];
NSString *username = [comment substringWithRange:wordRange];
NSLog(@"searchUsersInComment result --> %@", username);
}

Answer

(?<=^|(?<=[^a-zA-Z0-9-\\.]))@([A-Za-z]+[A-Za-z0-9-]+) is to neglect emails and grab only usernames, as your string doesn't contain any emails, you should just use @([A-Za-z]+[A-Za-z0-9-]+)

Your regex is wrong. You need to modify it to:

  NSString *comment = @"Hello, @username! How are you? And @username2??";

    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"@([A-Za-z]+[A-Za-z0-9-]+)" options:0 error:&error];
    NSArray *matches = [regex matchesInString:comment options:0 range:NSMakeRange(0, comment.length)];
    for (NSTextCheckingResult *match in matches) {
        NSRange wordRange = [match rangeAtIndex:1];
        NSString *username = [comment substringWithRange:wordRange];
        NSLog(@"searchUsersInComment result --> %@", username);
    }

FYI: Any subpattern inside a pair of parentheses will be captured as a group. In practice, this can be used to extract information like phone numbers or emails from all sorts of data. Imagine for example that you had a command line tool to list all the image files you have in the cloud. You could then use a pattern such as ^(IMG\d+.png)$ to capture and extract the full filename, but if you only wanted to capture the filename without the extension, you could use the pattern ^(IMG\d+).png$ which only captures the part before the period.

I would suggest you to read about regex strings: http://regexone.com/lesson/capturing_groups