Lord Zsolt
Lord Zsolt

Reputation: 6557

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?

Upvotes: 2

Views: 5049

Answers (3)

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

I think better you should use below like this:-

NSString *yourString  = @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";
NSArray *array = [yourString componentsSeparatedByString:@";"];
NSlog(@"%@",array);

Like that whatever format you want to split you can just pass special character into that componentsSeparatedByString method

Upvotes: 0

vikingosegundo
vikingosegundo

Reputation: 52227

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\\.]+)

Upvotes: 7

Nookaraju
Nookaraju

Reputation: 1668

  NSString *yourStr= @"Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)";

    NSArray *arrComponents = [yourStr componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/;()"]];

Upvotes: 3

Related Questions