Apple开发_提取出字符串中长度为24的UUID子字符串

@interface NSString (UUIDExtraction)

- (NSArray<NSString *> *)extract_UUID_Strings;

@end

@implementation NSString (UUIDExtraction)

// 提取出字符串中长度为24的UUID子字符串
- (NSArray<NSString *> *)extract_UUID_Strings {
    // 定义一个正则表达式来匹配UUID(这里假设没有连字符)
    NSString *uuid_pattern = @"[0-9A-F]{24}";
    NSError *error = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:uuid_pattern
                                                                           options:NSRegularExpressionCaseInsensitive error:&error];
    
    if (error) {
        NSLog(@"Error creating regular expression: %@", error.localizedDescription);
        return nil;
    }
    
    // 执行匹配并收集结果
    NSArray *matches = [regex matchesInString:self options:0 range:NSMakeRange(0, self.length)];
    NSMutableArray *uuids = [NSMutableArray array];
    
    for (NSTextCheckingResult *match in matches) {
        NSRange match_range = [match range];
        NSString *uuid = [self substringWithRange:match_range];
        [uuids addObject:uuid];
    }
    
    return uuids;
}

@end
posted @ 2024-08-02 18:07  CH520  阅读(2)  评论(0编辑  收藏  举报