jasonIM
jasonIM

Reputation: 193

String manipulation with regex and nsarray and mutable strings in objective C

I'm trying to parse data from text file in a very customised format.

 Ronaldo : 1, Messi : 1, Owen,Beckham : 2, Mario|Terry : 1, Lampard,Pele : 3

THis is whats in the text file, and I capture it using the regex pattern: ([^\r\n\\s]*)\\s*:\\s*([^\r\n\\s]*) //objectiveC

a simple NSLog(@"%@ = %@", group1, growp2) caputures between the : as keys and values.

Ronaldo = 1,
Messi = 1|2,
Owen,Beckham = 2
Mario|Terry = 1,
Lampard,Pele = 3
/* 
 `|` means "OR", Mario OR Terry = 1
 `,` means "AND", Lampard AND Pele = 3
 */

I wanted to now convert them into the following NSArray, which for the life of me, Im just clueless about.

array1 = ["Ronaldo=1" , "Messi=1" , "Owen,Becham=2", "Mario=1", "Lampard,Pele=3"]
array2 = ["Ronaldo=1", "Messi=1", "Owen,Becham=2", "Terry=1", "Lampard,Pele=3"]

What just happened is, the creation of two arrays based on the OR or | character.

Since Mario|Terry = 1, I wanted a set of array that contain each of the two independently.

Similarly, if there are three choices in another set, Mueller|Hargreves|Anelka, i should have three array sets with each of those, and no array with all of them.

Somehow, I'm not unable to translate it into code, so far, i've tried many things, with no success. Complexities are cropping up as for loops. for instance.

this is within the [NSRegularExpression enumerateMatchesInString...] code block

       for(NSString *player in OptionalPlayerArray)
       {
        NSMutableArray *optionalarr =[NSMutableArray new];


          for(NSString *value in valueChoices)
          {
            NSString *datastring = [NSString stringWithFormat:@"%@:%@",player,value]
            [optionalarr addObject:[datastring]];                                 
            }

       }

You see a double for-Loop, The valueChoices array also accounts for a choice/option in value. like Lampard = 1|2.

EDIT:

The nested for-loop was intended to relate to the optional-values. Lampard=1|2. I thought this would be easy to do "too". I was so wrong. So please ignore the nested loop, I can live without optional-values and instead focus entirely on optional-keys: Lampard|Mario=1.

But that got me no where. Please let me know what my options are, perhaps a different regex-approach? any help is appreciated.

Upvotes: 0

Views: 277

Answers (1)

Jeff Laing
Jeff Laing

Reputation: 923

Hopefully this isn't homework.

int main(int argc, const char * argv[])
{

@autoreleasepool {

    NSArray *data = @[
                @"Ronaldo : 1",
                @"Messi : 1",
                @"Owen,Beckham : 2",
                @"Mario|Terry : 1",
                @"Lampard,Pele : 3",
                @"Wally|Molly|Golly : 5",
                @"Homework : 0"
    ];

    // prime answers with a single empty entry
    __block NSMutableArray *answers = [NSMutableArray array];
    [answers addObject:[NSMutableArray array]];

    NSRegularExpression *re;
    re = [NSRegularExpression regularExpressionWithPattern:@"([^\r\n\\s]*)\\s*:\\s*([^\r\n\\s]*)"
                                                   options:0
                                                     error:nil];
    for (NSString *line in data) {
        [re enumerateMatchesInString:line
                             options:0
                               range:NSMakeRange(0, [line length])
                          usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
            {
                NSString *lhs = [line substringWithRange:[match rangeAtIndex:1]];
                NSString *rhs = [line substringWithRange:[match rangeAtIndex:2]];
                NSArray *players = [lhs componentsSeparatedByString:@"|"];
                if ([players count] == 1) {
                    NSString *p = [players objectAtIndex:0];
                    for (NSMutableArray *a in answers) {
                        [a addObject:[NSString stringWithFormat:@"%@=%@",p,rhs]];
                    }
                } else {
                    NSMutableArray *newanswers = [NSMutableArray array];
                    for (NSString *p in players) {
                        NSMutableArray *pa = [NSMutableArray array];
                        for (NSMutableArray *a in answers) {
                            [pa addObject:[a mutableCopy]];
                        }
                        for (NSMutableArray *a in pa) {
                            [a addObject:[NSString stringWithFormat:@"%@=%@",p,rhs]];
                        }
                        [newanswers addObjectsFromArray:pa];
                    }
                    answers = newanswers;
                }
            }
        ];
    }

    NSLog(@"%@",answers);
}
return 0;
}

Upvotes: 1

Related Questions