iOS正規表示式匹配

weixin_34320159發表於2016-03-08
- (NSString *)findHyperLinkWithString:(NSString *)rawString{
    if (rawString.length <= 0) {
        return @"";
    }
    NSError *error;
    NSRegularExpression *orderNumRegExp;
    
    NSString *orderNumRegExpStr = @"\\[.*:.*\\]"; //正則匹配表示式
    orderNumRegExp = [NSRegularExpression regularExpressionWithPattern:orderNumRegExpStr
                                                               options:0 error:&error];
    if (!error) {
        
        NSTextCheckingResult *matchResult = [orderNumRegExp firstMatchInString:rawString options:0 range:NSMakeRange(0, rawString.length)];
        
        if (matchResult) {
            NSString* matchedString = [rawString substringWithRange:matchResult.range];
//            NSRange range = [matchedString rangeOfString:@":"];
            
            return matchedString;
        }
    }
    return nil;
}

相關文章