Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find number pattern in string

I want to be able to find a substring within a sting but it has a distinctive pattern I am not sure how to find.

EX.

NSString *test1= @"Contact Names
                  67-444-322
                  Dec 21 2012
                  23941 6745 9145072 01567
                  5511 23345 614567 123456
                  Older Contacts
                  See Back Side";

I want to find the following pattern within the substring (these numbers but not date numbers)

                  23941 6745 9145072 01567
                  5511 23345 614567 123456

However, the format of the example string will hardly ever be the same. There will be different numbers and a different title every time other than "Contact Names", "Older Contacts" and "See Back Side". One thing that will remain constant is that the numbers I am looking for will always have 4 numbers but there could be 1 row or 10 rows.

Does anyone know how I would go about this problem? I was thinking something in terms of perhaps finding only the numbers within the string and then checking to see which numbers have 3 spaces in between.

Thanks

like image 827
Alex G Avatar asked Jan 14 '13 08:01

Alex G


People also ask

How do you match a string to a pattern?

Put brackets ( [ ] ) in the pattern string, and inside the brackets put the lowest and highest characters in the range, separated by a hyphen ( – ). Any single character within the range makes a successful match.

How do I extract all numbers from a string in Python?

This problem can be solved by using split function to convert string to list and then the list comprehension which can help us iterating through the list and isdigit function helps to get the digit out of a string.

What is the regex for string?

A regular expression (regex) defines a search pattern for strings. The search pattern can be anything from a simple character, a fixed string or a complex expression containing special characters describing the pattern.


2 Answers

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.)

like image 121
Attila H Avatar answered Oct 05 '22 23:10

Attila H


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 test1

for (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.

like image 25
Ephemera Avatar answered Oct 06 '22 00:10

Ephemera