Break it down.
I'm assuming you want to get the numeric values out of that string and toss the rest.
First make a regexp to match a number:
\d
matches a digit
\d*
matches zero or more digits
\.
matches a period
(\d*\.\d*)
matches zero or more digits, then a period, then zero or more digits
Then turn it into a Cocoa string:
Then make an NSRegularExpression
:
NSError error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\d*\\.\\d*)"
options:NSRegularExpressionCaseInsensitive
error:&error];
Then get an array with all the matches:
NSArray *matches = [regex matchesInString:string
options:0
range:NSMakeRange(0, [string length])];
Then pull out the values:
NSString* minText = [string substringWithRange:[matches[0] range]];
NSString* avgText = [string substringWithRange:[matches[1] range]];
// etc.
I leave it as an exercise to convert these strings into floats. :-)
Update: I found myself so nauseated by the complexity of this that I made a little library I am humbly calling Unsuck which adds some sanity to NSRegularExpression
in the form of from
and allMatches
methods. Here's how you'd use them:
NSRegularExpression *number = [NSRegularExpression from: @"(\\d*\\.\\d*)"];
NSArray *matches = [number allMatches:@"3.14/1.23/299.992"];
Please check out the unsuck source code on github and tell me all the things I did wrong :-)