Obj -c使用正则表达式用双引号分隔字符串

时间:2015-05-14 12:20:37

标签: ios objective-c regex

我今天刚刚学会了正则表达式,我有这个NSString:

{"Mon":["Operation System;Monday 1,2{9-16};ProfessorA;XX-229","Database;Monday3,4{1-16};ProfessorB;XX-130","Human Computer Interaction;Monday5,6{1-8};ProfessorC;XX-130;;C Programming;Monday5,6{9-16};ProfessorC;XX-336",null,null,null]}

我想将它们分开并放入NSArray这样的(分离后):

Mon
Operation System;Monday 1,2{9-16};ProfessorA;XX-229
Database;Monday3,4{1-16};ProfessorB;XX-130
Human Computer Interaction;Monday5,6{1-8};ProfessorC;XX-130;;C Programming;Monday5,6{9-16};ProfessorC;XX-336

并将其分别显示在我的TableViewCells中,我的正则表达式为:

[^\\"]

它完成了这项工作,但还不够好,因为这使它分成单个字母而不是我想要的字符串。有人有任何想法吗?

3 个答案:

答案 0 :(得分:1)

我已经阅读了这些评论,但我仍然有点困惑为什么你需要通过reg-exp手动解析它,即使你把它作为字符串...

所以,我提出这个,它可能是有用的(如果不适合你,但对于那些不想创建一个实际的reg-exp但想要通常解析它的人):

NSString *_json = @"{\"Mon\":[\"Operation System;Monday 1,2{9-16};ProfessorA;XX-229\",\"Database;Monday3,4{1-16};ProfessorB;XX-130\",\"Human Computer Interaction;Monday5,6{1-8};ProfessorC;XX-130;;C Programming;Monday5,6{9-16};ProfessorC;XX-336\",null,null,null]}";
NSError *_error = nil;
id _jsonId = [NSJSONSerialization JSONObjectWithData:[_json dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&_error];

然后你可以做任何你想要对这些值做的事情,因为_jsonId看起来像这样:

{
    Mon =     (
        "Operation System;Monday 1,2{9-16};ProfessorA;XX-229",
        "Database;Monday3,4{1-16};ProfessorB;XX-130",
        "Human Computer Interaction;Monday5,6{1-8};ProfessorC;XX-130;;C Programming;Monday5,6{9-16};ProfessorC;XX-336",
        "<null>",
        "<null>",
        "<null>"
    );
}

您可以手动提取,例如:

NSArray *_keyMon = [_jsonId valueForKey:@"Mon"];

NSString *_value0 = [_keyMon objectAtIndex:0]; // Operation System;Monday 1,2{9-16};ProfessorA;XX-229
NSString *_value1 = [_keyMon objectAtIndex:1]; // Database;Monday3,4{1-16};ProfessorB;XX-130
NSString *_value1 = [_keyMon objectAtIndex:2]; // Human Computer Interaction;Monday5,6{1-8};ProfessorC;XX-130;;C Programming;Monday5,6{9-16};ProfessorC;XX-336
// 3, 4, etc...
NSString *_value5 = [_keyMon objectAtIndex:5];

哪些值可能需要进一步的后处理,以便按;分隔组件,如:

NSArray *_value0Components = [_value0 componentsSeparatedByString:@";"];

将包含:

(
    "Operation System",
    "Monday 1,2{9-16}",
    "ProfessorA",
    "XX-229"
)

等...

答案 1 :(得分:0)

您只需要在引号之间匹配文字(编辑将捕获甚至转义的引号,测试包括\\\"Monday):

NSString *pattern = @"\"((?:[^\"\\\\]|\\\\.)*)\"";

以下示例代码可以帮助您取得进展:

NSError *error = nil;
NSString *pattern = @"\"((?:[^\"\\\\]|\\\\.)*)\"";
NSString *string = @"{\"Mon\":[\"Operation System;\\\"Monday 1,2{9-16};ProfessorA;XX-229\",\"Database;Monday3,4{1-16};ProfessorB;XX-130\",\"Human Computer Interaction;Monday5,6{1-8};ProfessorC;XX-130;;C Programming;Monday5,6{9-16};ProfessorC;XX-336\",null,null,null]}";
NSRange range = NSMakeRange(0, string.length);
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
NSArray *matches = [regex matchesInString:string options:0 range:range];
for (NSTextCheckingResult* match in matches) {
    NSRange group1 = [match rangeAtIndex:1];
    NSLog(@"group1: %@", [string substringWithRange:group1]);
}

Demo program输出:

2015-05-14 08:45:37.906 main[33] group1: Mon                                                                                                                        
2015-05-14 08:45:37.908 main[33] group1: Operation System;\"Monday 1,2{9-16};ProfessorA;XX-229                                                                        
2015-05-14 08:45:37.908 main[33] group1: Database;Monday3,4{1-16};ProfessorB;XX-130                                                                                 
2015-05-14 08:45:37.908 main[33] group1: Human Computer Interaction;Monday5,6{1-8};ProfessorC;XX-130;;C Programming;Monday5,6{9-16};ProfessorC;XX-336 

答案 2 :(得分:0)

对我和其他人来说,这看起来非常精致。试图用正则表达式解析它是纯粹的疯狂。 JSON将为您提供一个很好的字典,在“Mon”键下有一个字符串数组和NSNull值。你不能要求更好的东西。

(使用正则表达式的答案不会处理转义字符,会丢失数字,布尔值,空值,并且会丢失结构,所以一旦你得到键“Mon”和“Tue”就会被卡住)。

实际上,知道如何解析JSON对于任何程序员来说都是绝对必须的,所以你最好习惯它。