正则表达式(获取字符串中包含[]的内容)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *pattern = @"\\[\\w+\\]";
    NSString *str =@"[整单][123][微信][";

    NSRegularExpression *regx = [[NSRegularExpression alloc] initWithPattern:pattern
                                                                     options:NSRegularExpressionCaseInsensitive error:nil];
    // 查找匹配的字符串
    NSMutableArray *results = [[NSMutableArray alloc] init];
    NSRange searchRange = NSMakeRange(0, [str length]);
    
    [regx enumerateMatchesInString:str options:0 range:searchRange usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
        
        NSRange groupRange =  [result rangeAtIndex:0];
        NSString *match = [str substringWithRange:groupRange];
        [results addObject:match];
    }];
    NSLog(@"result = %@",results);

    
}

 

posted @ 2016-06-02 15:09  蜗牛d  阅读(4559)  评论(0编辑  收藏  举报