Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

iphone - 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:

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...