用于(Grails样式)键值对的值的ObjC格式正则表达式

时间:2013-12-11 16:31:57

标签: objective-c regex

假设一个属性文件如此(例如Grails中的application.properties):

app.name=An App Name
other.keys=Other Values
more.keys=More Values

我正在尝试编写一个Objective-C样式的正则表达式来提取值An App Name

到目前为止,使用一些示例代码对此进行测试,这是我所拥有的:

    NSError *errRegex = NULL;
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:@"^app.name=(.*?)$"
                                  options:NSRegularExpressionCaseInsensitive
                                  error:&errRegex];


    NSUInteger countMatches = [regex numberOfMatchesInString:strSource options:0 range:NSMakeRange(0, [strSource length])];
    NSLog(@"Number of Matches: %ld", countMatches);

    NSLog(@"-----");

    [regex enumerateMatchesInString:strSource options:0
                              range:NSMakeRange(0, [strSource length])
                         usingBlock:^(NSTextCheckingResult *match,
                                      NSMatchingFlags flags, BOOL *stop) {

                             NSLog(@"Ranges: %ld", [match numberOfRanges]);

                             NSString *matchFull = [strSource substringWithRange:[match range]];
                             NSLog(@"Match: %@", matchFull);

                             for (int i = 0; i < [match numberOfRanges]; i++) {
                                 NSLog(@"\tRange %i: %@", i,
                                       [strSource substringWithRange:[match rangeAtIndex:i]]);
                             }                              
                         }];

    if (errRegex) {
        NSLog(@"%@", errRegex);
    }

我得到以下输出:

2013-12-11 11:34:54.408 test[7321:303] Number of Matches: 1
2013-12-11 11:34:54.409 test[7321:303] -----
2013-12-11 11:34:54.409 test[7321:303] Ranges: 2
2013-12-11 11:34:54.410 test[7321:303] Match: app.name=An App Name        other.keys=Other     Values        more.keys=More Values
2013-12-11 11:34:54.410 test[7321:303]  Range 0: app.name=An App Name        other.keys=Other Values        more.keys=More Values
2013-12-11 11:34:54.410 test[7321:303]  Range 1: An App Name        other.keys=Other Values        more.keys=More Values

我假设我需要以换行符结束正则表达式,但我尝试过的任何内容都会返回0匹配。

1 个答案:

答案 0 :(得分:0)

尝试这种模式:

^app\.name=(.*)
相关问题