您也可以使用此模式:
[^&]&[^&]
并且调用一些特定于语言的SPLIT
函数应该可以完成这项工作。
如果这是针对 iOS 平台的,请在单独的应用程序中尝试此示例:
NSString *string = @"STRING1 && STRING2 & STRING3 AND STRING4"; //[NSString stringWithFormat:@"%@", @""];
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^&]&[^&]"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:string
options:0
range:NSMakeRange(0, [string length])];
NSLog(@"numberOfMatches : %d", numberOfMatches);
NSArray *matches = [regex matchesInString:string
options:0
range:NSMakeRange(0, [string length])];
for (NSTextCheckingResult *match in matches) // adjust loop per your criteria
{
NSRange matchRange = [match range];
// Pick the separator
NSLog(@"::::%@", [string substringWithRange:matchRange]);
// Splitted string array
NSArray *arr = [string componentsSeparatedByString:[string substringWithRange:matchRange]];
NSLog(@"Splitted String : %@", arr);
}