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

How can we split a complex string

<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"

我试过但没有成功

我是

先删除

'<PrintLetterBarcodeData' and '/>'

然后替换 '" ',即 quote-space 换行。

希望您没有 'val=" "',即以空白开头的值。

最好是正则表达式,它匹配 word="val".

看看这个:

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]);
}];