我们如何分割复杂的字符串

时间:2015-05-05 12:41:57

标签: ios objective-c nsstring nsarray string-split

<PrintLetterBarcodeData uid="229636134720" name="Nandan Mohan Nilekani" 
     gender="M" yob="1955" house="856, 13th Main , 3rd Block" 
     loc="Koramangala" vtc="Bangalore South" po="Koramangala" 
     dist="Bangalore" state="Karnataka" pc="560034"/>

我想将上面的字符串拆分为

uid="229636134720"
name="Nandan Mohan Nilekani"
gender="M"
yob="1955"
house="856, 13th Main , 3rd Block"
loc="Koramangala"
vtc="Bangalore South"
po="Koramangala"
dist="Bangalore"
state="Karnataka"
pc="560034"

我尝试过但未能像那样做

2 个答案:

答案 0 :(得分:1)

看看这个:

NSString *testString = @"<PrintLetterBarcodeData uid=\"229636134720\" name=\"Nandan Mohan Nilekani\" gender=\"M\" yob=\"1955\" house=\"856, 13th Main , 3rd Block\" loc=\"Koramangala\" vtc=\"Bangalore South\" po=\"Koramangala\" dist=\"Bangalore\" state=\"Karnataka\" pc=\"560034\"/>";

NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@" \\w+=\".+?\"" options:0 error:nil];

[regex enumerateMatchesInString:testString options:0 range:NSMakeRange(0, testString.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    NSLog(@"%@", [testString substringWithRange:result.range]);
}];

答案 1 :(得分:-1)

我是

首先删除

'<PrintLetterBarcodeData' and '/>'

然后用换行符替换'“',即引号空格。

希望你没有'val =“”',即一个以空白开头的值。

最好是正则表达式,匹配word =“val”。