Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSString regex string matching/search

I have a string like @"random ++qwerty/asdf" and I want to fish out the qwerty part. The "random ++" and "/asdf" will always be the same. How would I go about doing this using regular expressions? I'm confused as to how they work.

like image 817
Hudson Buddy Avatar asked Dec 08 '22 16:12

Hudson Buddy


1 Answers

Problem Solved !!

This code give exactly what you want,

NSString *yourString = @"random ++qwerty/asdf random ++derty/asdf random ++noty/asdf";
NSError *error = NULL;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"random \\+\\+(\\w+)/asdf" options:NSRegularExpressionCaseInsensitive error:&error];


[regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){

    // detect
    NSString *insideString = [yourString substringWithRange:[match rangeAtIndex:1]];

    //print
    NSLog(@"%@",insideString);

}];


Output:
qwerty
derty
noty

like image 109
Thilina Chamath Hewagama Avatar answered Dec 11 '22 10:12

Thilina Chamath Hewagama