0

我正在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 值,但不是最终的字符串。我究竟做错了什么?

4

1 回答 1

1

您的模式以(%@). 我应该以(.+)相反的方式结束。

于 2013-02-08T21:53:17.460 回答