25

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:

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,

0

2 Answers 2

91

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
0
1

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.

2
  • 3
    i think NSRegularExpression more useful Nov 6, 2011 at 17:54
  • Compared to Perl or Ruby or many other languages, NSRegularExpression is powerful but in the most common cases uses a paragraph to do a sentence's worth of work. I like how this library example can let you match an expression and capture a group in one reasonable line. Feb 11, 2014 at 19:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.