字符串解析,通过逗号分开的,除非它是由引号包围(string parsing, separate b

2019-09-29 05:08发布

我需要解析以下字符串在Objective-C的iOS应用

的NSString * htmlString = @ “12,22, 'stringA', '', 'stringB,stringC',2 'STRING天'”;

我想这样的一个数组

{
    @12,
    @22,
    @"stringA",
    @"emptySlotInfo",
    @"stringB, stringC",
    @2,
    @"stringD"
}

头痛是@“strinb,stringC”,因为

[htmlString componentsSeparatedByString:@","];

不适合的情况下和@“'”工作作为分隔符也不起作用。

我怎样才能获得必要的组件?

Answer 1:

你可以使用NSScanner 。

如果扫描'它知道一个字符串开始和忽视,直到它读取下一个' 。 如果没有开放'被读取,sperate的,

这cocoawithlove文章可能会有所帮助。


我做了一个快速的原型。 最有可能有很多优化,因为我还没有为NSScanner一个专家

NSString *htmlString = @"12, 22, 'stringA','', 'stringB, stringC', 2,'stringD'";
NSScanner *scanner = [NSScanner scannerWithString:htmlString];

NSString *apostrophe = @"'";    // scanner needs to detect this
NSString *comma = @",";         // scanner needs to detect this
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:[NSString stringWithFormat:@"%@%@", apostrophe, comma]];
BOOL apostropheOpen = NO;       // is the scan location inside a single quoted substring?
NSInteger lastCommaIndex = -1;  // track last found comma's index
NSMutableArray *array = [NSMutableArray array];

while (![scanner isAtEnd]) {
    [scanner scanUpToCharactersFromSet:charSet intoString:NULL];
    NSString *charAtlocation = [htmlString substringWithRange:NSMakeRange([scanner scanLocation], 1)];
    if ([charAtlocation isEqualToString:apostrophe]){
        apostropheOpen = !apostropheOpen;                
    } else if ([charAtlocation isEqualToString:comma]){
        if (!apostropheOpen) {
            [array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
            lastCommaIndex = [scanner scanLocation];
        }
    }
    [scanner setScanLocation:[scanner scanLocation]+1];
} ;

// scanner only dealt with the string until the last comma, probably one more value to handle
if (lastCommaIndex < [scanner scanLocation]){
    [array addObject: [scanner.string substringWithRange:NSMakeRange(lastCommaIndex+1, [scanner scanLocation]- lastCommaIndex-1)]];
}

// array contains seperated strings, but with blanks and apostrophes
// we will deal with them now
__block NSMutableArray *resultArray = [NSMutableArray array];
[array enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
    obj = [[obj stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
                stringByTrimmingCharactersInSet:charSet];
    if ([obj length] > 0)
        [resultArray addObject:obj];
    else
        [resultArray addObject:@"emptySlotInfo"];
}];

该resultArray包含

(
12,
22,
stringA,
emptySlotInfo,
stringB, stringC,
2,
stringD
)


文章来源: string parsing, separate by comma, unless it is enclosed by apostrophes