2013-02-08 82 views
0

我修剪的NSString通過以下方式:修剪的NSString

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"X = (\\d+.\\d+), Y = (\\d+.\\d+), (%@)" options:0 error:NULL]; 
NSTextCheckingResult *match = [regex firstMatchInString:txt.text options:0 range:NSMakeRange(0, [txt.text length])]; 
if (match) { 
    NSRange firstHalfRange = [match rangeAtIndex:1]; 
    NSString *xString = [txt.text substringWithRange:firstHalfRange]; 
    NSRange secondHalfRange = [match rangeAtIndex:2]; 
    NSString *yString = [txt.text substringWithRange:secondHalfRange]; 
    NSRange thirdHalfRange = [match rangeAtIndex:3]; 
    NSString *colorString = [txt.text substringWithRange:thirdHalfRange]; 

    NSLog(@"X = %@, Y = %@, Color: %@", yString, xString, colorString); 
} 

NSString看起來是這樣的:

@"this is the content. The content of this string may vary, as well as the length, and my include any characters, with numbers X = 295.000000, Y = 207.500000, TheWhite" 

的部分Y = 295.000000 X = 207.500000總是保持相同,除了可能變化的X和Y數字,以及:TheWhite,它可能成爲例如TheBlack

使用此方法,我成功提取X和Y值,但不是最終字符串。我究竟做錯了什麼?

回答

1

您的模式以(%@)結尾。相反,我應該以(.+)結束。