How to get matches in iOS using regular expression?

I got a string like ‘stackoverflow.html’ and in the regular expression ‘stack(.).html’ I would like to have the value in (.).

I could only find NSPredicate like:

  • Remove space between sections in collectionview
  • Why is the icon for my iPhone app crossed-out after a build?
  • How to use UIPickerView to populate different textfields in one view?
  • UIWebview resize height to fit content
  • Rotate portrait to landscape and portrait mode of my view controller
  • What is the advantage of using armv7s support In Valid Architecture?
  • NSString    *string     = @"stackoverflow.html";
    NSString    *expression = @"stack(.*).html";
    NSPredicate *predicate  = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", expression];
    BOOL match = [predicate evaluateWithObject:string]
    

    But that only tells I got a match and doesn’t return a string, when I use NSRegularExpression:

    NSRange range = [string rangeOfString:expression options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
    if (range.location == NSNotFound) return nil;
    
    NSLog (@"%@", [string substringWithRange:(NSRange){range.location, range.length}]);
    

    It will give me the total string back, stackoverflow.html, but I am only interested in that whats in (.*). I want to get ‘overflow’ back. In PHP this was easy to do, but how can you accomplish this in xCode for iOS?

    Logically if I would do this:

    NSInteger firstPartLength  = 5;
    NSInteger secondPartLength = 5;
    NSLog (@"%@", [string substringWithRange:(NSRange){range.location + firstPartLength, range.length - (firstPartLength + secondPartLength)}]
    

    It gives me the propert result ‘overflow’. But the problem is in many cases I dont know the length of the first part or the second part. So is there a way I can get the value that should be in (.*) ?

    Or must I decide to choose the ugliest method by finding the location of (.) and calculate the first and second part from there? But in regular expression you might also have ([a-z]) but then the ugly way of using another regular expression to get the location of values between () and then use that to calculate the left and right part? And what happends if I have more? like ‘A (.) should find the answer to (.*).’ I would like to have an array as result with values [0] the value after A and [1] the value after to.

    I hope my question is clear.

    Thanks in advance,

    2 Solutions Collect From Internet About “How to get matches in iOS using regular expression?”

    in iOS 4.0+, you can use NSRegularExpression:

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"stack(.*).html" options:0 error:NULL];
    NSString *str = @"stackoverflow.html";
    NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
    // [match rangeAtIndex:1] gives the range of the group in parentheses
    // [str substringWithRange:[match rangeAtIndex:1]] gives the first captured group in this example
    

    You want the RegexKitLite library in order to perform regular expression matches:

    http://regexkit.sourceforge.net/RegexKitLite/

    After that it’s almost exactly like you do it in PHP.

    I’ll add some code to help you with it:

    NSString *string     = @"stackoverflow.html";
    NSString *expression = @"stack(.*)\\.html";
    NSString *matchedString = [string stringByMatching:expression capture:1];
    

    matchedString is @”overflow”, which should be exactly what you need.