Find number pattern in string Find number pattern in string objective-c objective-c

Find number pattern in string


I've tried the following and it works:

NSString *test1= @"Contact Names\n"    "67-444-322\n"    "Dec 21 2012\n"    "23941 6745 9145072 01567\n"    "5511 23345 614567 123456\n"    "Older Contacts\n"    "See Back Side";NSString *pattern = @"(([0-9]+ ){3}+[0-9]+)(\\n(([0-9]+ ){3}+[0-9]+))*";NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];NSArray *results = [regex matchesInString:test1 options:0 range:NSMakeRange(0, [test1 length])];if ([results count] > 0) {    NSTextCheckingResult *result = [results objectAtIndex:0];    NSString *match = [test1 substringWithRange:result.range];    NSLog(@"\n%@", match); // These are your numbers}

(It also works if there is only one line of numbers.)


You could use character sets to separate the string and then determine if there are 4 numbers in each component. This will only work though if the string has newline characters (\n) in it (as your response to Lance seems to indicate).

This is how I would go about it:

NSString *test1= @"Contact Names\n              67-444-322\n              Dec 21 2012\n              23941 6745 9145072 01567\n              5511 23345 614567 123456\n              Older Contacts\n              See Back Side";NSArray *lines = [test1 componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet];// lines now contains each line in test1for (NSString* line in lines) {    NSArray *elements = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet];    if (elements.count == 4) {        // This line contains 4 numbers        // convert each number string into an int if needed    }}

Sorry about the long code lines, some of Apple's selectors are a little on the long side... In any case, if elements has 4 individual (NSString) objects, then it is one of the lines you are seeking and you can manipulate the data as you need.

EDIT (aside):

On the topic of Regex (as this question contains the regex tag), yes you could use regular expressions, but Objective-C doesn't really have a 'nice' way of handling them... Regex is more in the domain of scripting languages and languages which have built-in support for it.


I refined my code to be more readable and to stop when it finds the string(doesnt break into lines...if you need this too tell me to add the code again or help you if having difficulty doing it)

The regular expression i used is:
-One or more numbers followed by one or more spaces (tree times all this)
-One or more numbers followed by one or more whitespaces(theses are line changes,tabs,spaces ,etc)
-I try to find this whole pattern be repeated 1 or more times

The code is

NSString *test1= @"Contact Names\n    67-444-322\n\nDec 21 2012\n23941 6745 9145072 01567\n5511 23345 614567 123456\nOlder Contacts\nSee Back Side\n";//create the reg exprNSString *pattern1 = @"(([0-9]+ +){3}[0-9]+\\s+)+";NSRegularExpression *regex1 = [NSRegularExpression regularExpressionWithPattern:pattern1 options:0 error:nil];//find matchesNSArray *results1 = [regex1 matchesInString:test1 options:0 range:NSMakeRange(0, [test1 length])];if ([results1 count] > 0) {    //if i find more series...what should i do?    if ([results1 count] > 1) {        NSLog(@"I found more than one matching series....what should i do?!");        exit(111);    }    //find series and print    NSTextCheckingResult *resultLocation1 = [results1 objectAtIndex:0];    NSString *match1 = [test1 substringWithRange:resultLocation1.range];    //trim leading and ending whitespaces    match1=[match1 stringByTrimmingCharactersInSet:            [NSCharacterSet whitespaceAndNewlineCharacterSet]];    NSLog(@"the series is \n%@", match1);        }else{    NSLog(@"No matches found in string");}

Hope it helps