7

これらは私のサンプル NSStrings (配列に解析されたもの) で、単純化されています。

単一の文字列オブジェクトは、「STRING1、STRING2、DATE TIME、A:VALUE」のように解析されます。例:

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

さて、STRING または STRING、STRING の個別の値を抽出する必要があります。

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

次にDATEと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

最後に、単一の値:

30.0
20.0
40.0
50.0
10.0

編集:解析された *allValues に Martin R のコードを使用しようとしています:

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

単一の値を抽出するコードは次のとおりです。

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);
    }
}

しかし、この場合、NSLog の結果を取得できません ( match == nil )。何が起こったか?ありがとう!

4

2 に答える 2

15

DATE/TIME の形式が固定されている場合は、正規表現を使用できます。

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);
}

出力:

最初の部分: 最初の値の名前、最初
後編: 2013/04/17 11:30:00
第三部: 30.0
于 2013-04-17T11:56:12.853 に答える
-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.

于 2013-04-17T11:50:05.103 に答える