使用正则表达式拆分字符串 - objective-C

Lor*_*olt 2 regex split objective-c

我对正则表达式很陌生,我正在努力学习它.

这是我的字符串:

Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)
Run Code Online (Sandbox Code Playgroud)

我想把它分成一个看起来像这样的数组:

@[@"Mozzila", @"4.0", @"compatible", @"MSIE 5.0", @"Windows NT", @"DigExt"];
Run Code Online (Sandbox Code Playgroud)

这是我试过的代码:

NSString *expression = @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSRegularExpression *testExpression = [NSRegularExpression regularExpressionWithPattern: @"([a-zA-Z]+)/([1-9.]+) \(([a-z]+); ([a-zA-Z .]+); ([a-zA-Z ]+); ([a-zA-Z]+)\)" options:NSRegularExpressionCaseInsensitive error:nil];
                                                                                options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [testExpression matchesInString:expression 
                                           options:0 
                                             range:NSMakeRange(0, [expression length])];
NSLog(@"%@",matches);
Run Code Online (Sandbox Code Playgroud)

还试过:

[testExpression enumerateMatchesInString:expression
                                 options:0
                                   range:NSMakeRange(0, [expression length])
                              usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                  NSLog(@"Value: %@", [expression substringWithRange:[result rangeAtIndex:1]]);
                              }];
Run Code Online (Sandbox Code Playgroud)

并且:

NSRegularExpression *testExpression = [NSRegularExpression
                                       regularExpressionWithPattern: @"(\w+)/(\w+) \((\w+);([\w .]+); ([\w ]+); (\w+)\)" options:NSRegularExpressionCaseInsensitive
                                       error:nil];
Run Code Online (Sandbox Code Playgroud)

但是日志是空的.我究竟做错了什么?

Vik*_*ica 6

NSString *expression = @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSRegularExpression *testExpression = [NSRegularExpression regularExpressionWithPattern:@"(.+)/([0-9\\.]+) \\(([^)]*).*"
                                                                                options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *matches = [testExpression matchesInString:expression
                                           options:0
                                             range:NSMakeRange(0, [expression length])];
NSLog(@"%@",matches);

NSMutableArray *array = [@[] mutableCopy];
[matches enumerateObjectsUsingBlock:^(NSTextCheckingResult *obj, NSUInteger idx, BOOL *stop) {

    for (int i = 1; i< [obj numberOfRanges]; ++i) {
        NSRange range = [obj rangeAtIndex:i];

        NSString *string = [expression substringWithRange:range];
        if ([string rangeOfString:@";"].location == NSNotFound) {
            [array addObject: string];
        } else {
            NSArray *a = [string componentsSeparatedByString:@";"];
            for (NSString *s  in a) {
                [array addObject: [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
            }

        }

    }
}];
Run Code Online (Sandbox Code Playgroud)

array 包含

<__NSArrayM 0x10010d540>(
Mozilla,
4.0,
compatible,
MSIE 5.0,
Windows NT,
DigExt
)
Run Code Online (Sandbox Code Playgroud)
@"(.+)/([0-9\\.]+) \\(([^)]*).*"
  ^__^                           capture group 1
       ^_________^               capture group 2
                     ^           the char (
                      ^_____^    capture group 3
Run Code Online (Sandbox Code Playgroud)
  • 捕获组1捕获所有可打印的字符,直到/.
  • 捕获组2捕获所有数字和点.我们必须逃避点\\,否则它将再次代表任何角色.
  • \\(他说(会跟随,但是因为我们没有将它包含在我们的捕获组中,所以我们并不关心它.
  • 捕获组3 ([^)]*)说"任何可打印的但不是)

现在我们用它们的范围迭代捕获组.我们从索引1开始,因为索引0将给出完整表达式的范围


([1-9.]+)
Run Code Online (Sandbox Code Playgroud)

这将不匹配0,点代表任何可打印的字符.你要

([0-9\\.]+)
Run Code Online (Sandbox Code Playgroud)