7

这些是我简化的示例 NSStrings(解析为数组)。

单个字符串对象被解析为 "STRING1, STRING2 then DATE TIME then - 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

然后是日期和时间

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

编辑:我正在尝试使用 Martin R 的代码来解析 *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"
    )

这是提取单个值的代码:

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

输出:

第一部分:第一个值的名称,first
第二部分:17/04/2013 11:30:00
第三部分:30.0
于 2013-04-17T11:56:12.853 回答
-1

NSString 类提供了许多不同的方法来完成你想要的。查找、组合和划分字符串和子字符串。看看这些方法,我相信你可以将它们组合起来得到你想要的字符串。我认为您将需要一些 if() else 大括号。

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

希望这对您有所帮助。

于 2013-04-17T11:50:05.103 回答