7

These are my sample NSStrings (parsed into an array), simplified.

The single string object is parsed like "STRING1, STRING2 then DATE TIME then - A:VALUE", ex:

The name of first value, first 17/04/2013 11:30:00 - A:30.0 // sometimes there is a comma after first string
The name of second value 17/04/2013 11:00:00 - A:20.0 // sometimes there is NOT a comma after first string
Name of third value 17/04/2013 10:30:00 - A:40.0
Fourth 17/04/2013 09:40:00 - A:50.0
Fifth value 17/04/2013 05:00:00 - A:10.0

Well, I need to extract the separate values of STRING or STRING, STRING:

The name of first value, first
The name of second value
Name of third value
Fourth
Fifth value

then DATE and TIME

17/04/2013 11:30:00
17/04/2013 11:00:00
17/04/2013 10:30:00
17/04/2013 09:40:00
17/04/2013 05:00:00

finally, the single VALUES:

30.0
20.0
40.0
50.0
10.0

EDIT: I'm trying to use a code from Martin R for my parsed *allValues:

The *allValues array is something like: 
    (
        "The name of first value, first 17/04/2013 11:30:00 - A:30.0",
        "The name of second value 17/04/2013 11:00:00 - A:20.0",
        "Name of third value 17/04/2013 10:30:00 - A:40.0",
        "Fourth 17/04/2013 09:40:00 - A:50.0",
        "Fifth value 17/04/2013 05:00:00 - A:10.0"
    )

and here is the code to extract single values:

NSMutableArray *allValues = [parsedFeed valueForKey:@"values"];

for (int i=1; i < [allValues count]; i++) {
    NSString *string = [allValues objectAtIndex:i];
    NSString *pattern = @"(.*) (\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}) - A:(.*)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
    NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];

    if (match != nil) {
    NSString *s1 = [string substringWithRange:[match rangeAtIndex:1]];
    NSString *s2 = [string substringWithRange:[match rangeAtIndex:2]];
    NSString *s3 = [string substringWithRange:[match rangeAtIndex:3]];
    NSLog(@"First part:  %@", s1);
    NSLog(@"Second part: %@", s2);
    NSLog(@"Third part:  %@", s3);
    }
}

But in this case I cannot get any NSLog result (match == nil). What's happened? Thanks!

SILminore
  • 509
  • 3
  • 10
  • 28
  • [What have you tried?](http://whathaveyoutried.com) We are not here to solve your problems for you but to help you with particular issues you are stuck on while trying to solve a problem. You also might want to have a look at the [FAQ](http://stackoverflow.com/questions/how-to-ask) – Martin Ender Apr 17 '13 at 11:44

2 Answers2

16

If the DATE/TIME has a fixed format, you can use a regular expression:

NSString *string = @"The name of first value, first 17/04/2013 11:30:00 - A:30.0";
NSString *pattern = @"(.*) (\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}) - A:(.*)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                       options:0 error:NULL];
NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
if (match != nil) {
    NSString *s1 = [string substringWithRange:[match rangeAtIndex:1]];
    NSString *s2 = [string substringWithRange:[match rangeAtIndex:2]];
    NSString *s3 = [string substringWithRange:[match rangeAtIndex:3]];
    NSLog(@"First part:  %@", s1);
    NSLog(@"Second part: %@", s2);
    NSLog(@"Third part:  %@", s3);
}

Output:

First part:  The name of first value, first
Second part: 17/04/2013 11:30:00
Third part:  30.0
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thank you very much @Martin R, your code is perfect for ONE fixed string, but if you see the EDIT on my question, I cannot use it to extract values from the strings of a NSMutableArray... is there some issue? – SILminore Apr 17 '13 at 12:36
  • @Huxley: Note that your loop starts with index 1 instead of 0, and that your first string uses a dot (.) and not a colon (:) in the time field. My regex checks for a colon. One could generalize it to check for dot or colon. - Apart from that, I don't see a problem with arrays, and your EDITED code works without problems in my test app. – Martin R Apr 17 '13 at 12:52
  • Perfect, Martin, it was a typo error! :) Last double, whats the way to "generalize" checks for dot or colon, for example, can u provide the code? Thank you again! – SILminore Apr 17 '13 at 12:59
  • 2
    @Huxley: Replace `:` by `(?:\\.|:)` in the regex. (I should charge bonus points for that :-) – Martin R Apr 17 '13 at 13:07
-1

There are a lot of different methods provided by the NSString Class to get done what you want. Finding, Combining and Dividing strings and substrings. have a look at these methods, I'm sure you can combine them to get the strings you want. I think you will need some if() else braces though.

http://developer.apple.com/library/ios/#Documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

Hope you this helps you.

Heckscheibe
  • 630
  • 7
  • 8