Aashi
Aashi

Reputation: 170

How to Split String With Regular Expression ios objectivec

Below is the Syntax of Java : Code.split("(?<=\\D)(?=\\d)");

I want to convert this in ios objective c

string Code : LR00001 or BLNS-9-M

I want O/p as:

This o/p is working in Java but want in ios objective c.

This regular expression: (?<=\\D)(?=\\d) means it finds position in the string where there is a non-digit (\D) character before that position and a digit (\d) after it. i.e matches a position between a non-digit (\D) and a digit (\d).

Following Code I have tried :

1.

NSArray *arrComponents = [strCode componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"(?<=\\D)(?=\\d)"]];

2.

NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\\D)(?=\\d)" options:0 error:&error];
NSArray* matches = [regex matchesInString:strCode options:0 range:NSMakeRange(0, [strStyleCode length])];
NSTextCheckingResult *match = [regex firstMatchInString:strCode options:0 range: NSMakeRange(0, [strStyleCode length])];
NSLog(@"group1: %@", [strCode substringWithRange:[match rangeAtIndex:0]]);
NSLog(@"group2: %@", [strCode substringWithRange:[match rangeAtIndex:1]]);

3.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"(?<=\\D)(?=\\d)"];

All of the above do not work.

I want the same output in ios objective c.

[LR,00001] [BLNS,9-M]

Upvotes: 1

Views: 1205

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626871

You may first replace all the matches with a non-used symbol, say, with \x00 null char, and then split with it:

NSError *error = nil;
NSString *str = @"LR00001";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=\\D)(?=\\d)" options:nil error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:str options:0 range:NSMakeRange(0, [str length]) withTemplate:@"\x00"];
NSArray *chunks = [modifiedString componentsSeparatedByString: @"\x00"];
for(NSString *c in chunks) {
    NSLog(@"%@", c);
}

It prints LR and 00001.

See the online demo.

Upvotes: 2

CRD
CRD

Reputation: 53010

Java's String.split uses a regular expression to match the delimiter, possibly zero-length, at which the string should be split. It then extracts the parts of the string before and after the match, it doesn't extract anything the RE matched.

Objective-C has methods to divide a string, componentsSeparatedBy..., but none of these take a regular expression. Your first attempt tries to use one of these by defining a character set using your regular expression – however you cannot define a character set this way and even if you could the set of characters in your particular delimiter is empty.

Your third attempt is incomplete so we'll skip that.

Now your second attempt locates the delimiter using firstMatchInString:, but having found it does not try to extract the strings before and after that delimiter, i.e. you don't perform an actual split. If you do that you have the makings of an Objective-C equivalent of Java's split.

In outline:

  1. Use firstMatchInString: to locate the first delimiter in your string
  2. If the match fails you've finished, add the remaining string to your collection of components and return.
  3. If the match succeeds then:
    1. extract the substring up to the start of the delimiter match – part of the result of firstMatchInString: is an NSRange, that contains a starting index, and NSString has methods to extract substrings based on indexes.
    2. extract the substring after the delimiter match – similar to the above but remember to allow for the length of the match when calculating the index where this substring starts
    3. you now need to split this second substring, so go back to step 1 and repeat the process using this substring.

You can code the above as a simple while loop in Objective-C.

If while trying to implement this algorithm you get stuck ask a new question, show your code, explain your issue, include a link back to this question, and someone will undoubtedly help you.

HTH

Upvotes: 1

Related Questions