@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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.


作者: CH520