【问题标题】:Extract parts from regular expression with NSRegularExpression使用 NSRegularExpression 从正则表达式中提取部分
【发布时间】:2012-03-24 23:38:38
【问题描述】:

我想用 NSRegularExpression 提取字符串的一部分。

例如,我有这个字符串:

@"1   UIKit                               0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163";

我想提取“UIKit”、“0x00540c89”、“UIApplication”、“_callInitializationDelegatesForURL:payload:suspended:”和“1163”。

我已经做了正则表达式:

@"^[0-9]+\\s+[a-zA-Z]+\\s+0x[0-9a-zA-Z]+\\s+\\-\\s*\\[[a-zA-Z]+\\s+[_:a-zA-Z]+\\]\\s+\\+\\s+[0-9]+"

但我不知道该怎么做。这是可能的。

    NSString *origen = @"1   UIKit                               0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163";
    // Setup an NSError object to catch any failures
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]+\\s+[a-zA-Z]+\\s+0x[0-9a-zA-Z]+\\s+\\-\\s*\\[[a-zA-Z]+\\s+[_:a-zA-Z]+\\]\\s+\\+\\s+[0-9]+" 
                                                                           options:NSRegularExpressionCaseInsensitive 
                                                                             error:&error];
    // create an NSRange object using our regex object for the first match in the string
    NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:origen options:0 range:NSMakeRange(0, [origen length])];
    // check that our NSRange object is not equal to range of NSNotFound
    if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
        // Since we know that we found a match, get the substring from the parent string by using our NSRange object
        NSString *substringForFirstMatch = [origen substringWithRange:rangeOfFirstMatch];
        NSLog(@"Extracted: %@",substringForFirstMatch);
    }

【问题讨论】:

    标签: objective-c regex nsregularexpression


    【解决方案1】:

    您显然需要一种方法来匹配多个范围与您的正则表达式。这是通过用括号表示的匹配组来完成的。然后,您可以使用NSRegularExpression 方法之一,该方法为您提供NSTextCheckingResult 而不是简单的范围。 NSTextCheckingResult 可以包含多个范围。

    例子:

    NSString *pattern = @"^[0-9]+\\s+([a-zA-Z]+)\\s+(0x[0-9a-zA-Z]+)\\s+\\-\\s*(\\[[a-zA-Z]+\\s+[_:a-zA-Z]+\\])\\s+\\+\\s+([0-9]+)";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern 
                                                                           options:NSRegularExpressionCaseInsensitive 
                                                                             error:&error];
    
    NSTextCheckingResult *firstResult = [regex firstMatchInString:origen options:0 range:NSMakeRange(0, origen.length)];
    if ([firstResult numberOfRanges] == 5) {
        //The range at index 0 contains the entire string.
        NSLog(@"1: '%@'", [origen substringWithRange:[firstResult rangeAtIndex:1]]);
        NSLog(@"2: '%@'", [origen substringWithRange:[firstResult rangeAtIndex:2]]);
        NSLog(@"3: '%@'", [origen substringWithRange:[firstResult rangeAtIndex:3]]);
        NSLog(@"4: '%@'", [origen substringWithRange:[firstResult rangeAtIndex:4]]);
    }
    

    【讨论】:

      【解决方案2】:

      试试这个:

      NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
      NSMutableArray *array = [origen  componentsSeparatedByCharactersInSet:separatorSet];
      [array removeObject:@""];
      

      【讨论】:

        猜你喜欢
        • 2021-08-22
        • 1970-01-01
        • 2018-11-04
        • 1970-01-01
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多