Reputation: 170
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:
if code is LR00001 then Array gives me two values. On 0th index it gives LR and in 1 index it gives me 00001
if code is BLNS-9-M then Array[0] BLNS and Array[1] 9-M
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
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
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:
firstMatchInString:
to locate the first delimiter in your stringfirstMatchInString:
is an NSRange
, that contains a starting index, and NSString
has methods to extract substrings based on indexes.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