Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C NSRegularExpressions, finding first occurrence of numbers in a string

I'm pretty green at regex with Objective-C. I'm having some difficulty with it.

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b([1-9]+)\\b" options:NSRegularExpressionCaseInsensitive error:&regError];
if (regError) {
    NSLog(@"%@",regError.localizedDescription);
}
__block NSString *foundModel = nil;
[regex enumerateMatchesInString:self.model options:kNilOptions range:NSMakeRange(0, [self.model length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
    foundModel = [self.model substringWithRange:[match rangeAtIndex:0]];
    *stop = YES;
}];

All I'm looking to do is take a string like

150A

And get

150
like image 620
rnystrom Avatar asked Oct 26 '12 18:10

rnystrom


1 Answers

First the problems with the regex:

  1. You are using word boundaries (\b) which means you are only looking for a number that is by itself (e.g. 15 but not 150A).
  2. Your number range does not include 0 so it would not capture 150. It needs to be [0-9]+ and better yet use \d+.

So to fix this, if you want to capture any number all you need is \d+. If you want to capture anything that starts with a number then only put the word boundary at the beginning \b\d+.

Now to get the first occurrence you can use
-[regex rangeOfFirstMatchInString:options:range:]

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\b\\d+" options:NSRegularExpressionCaseInsensitive error:&regError];
if (regError) {
    NSLog(@"%@",regError.localizedDescription);
}

NSString *model = @"150A";
NSString *foundModel = nil;
NSRange range = [regex rangeOfFirstMatchInString:model options:kNilOptions range:NSMakeRange(0, [model length])];
if(range.location != NSNotFound)
{
    foundModel = [model substringWithRange:range];
}

NSLog(@"Model: %@", foundModel);
like image 194
Joe Avatar answered Sep 19 '22 18:09

Joe