Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string with regular expression - objective-C

I'm quite new to regular expression and I'm trying to learn it.

Here's my string:

Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)

And I want to split it up to an array which looks like this:

@[@"Mozzila", @"4.0", @"compatible", @"MSIE 5.0", @"Windows NT", @"DigExt"];

This is the code I tried:

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);

Also tried with:

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

And also:

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

But the log is empty. What am I doing wrong?

like image 359
Lord Zsolt Avatar asked Aug 02 '26 03:08

Lord Zsolt


1 Answers

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]]];
            }

        }

    }
}];

array contains

<__NSArrayM 0x10010d540>(
Mozilla,
4.0,
compatible,
MSIE 5.0,
Windows NT,
DigExt
)

@"(.+)/([0-9\\.]+) \\(([^)]*).*"
  ^__^                           capture group 1
       ^_________^               capture group 2
                     ^           the char (
                      ^_____^    capture group 3
  • Capture group 1 captures all printable chars till /.
  • Capture group 2 captures all numbers and dots. we must escape the dot with \\, as otherwise it would again stand for any character.
  • \\( says that a ( will follow, but as we dont enclose it it our capture groups, we just dont care much for it.
  • Capture group 3 ([^)]*) says "anything printable but not )

Now we iterate over the capture groups with their ranges. we start at index 1, as index 0 will give the range of the complete expression


([1-9.]+)

this will not match 0 and the points stands for any printable character. you want

([0-9\\.]+)
like image 105
vikingosegundo Avatar answered Aug 03 '26 18:08

vikingosegundo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!