我刚刚开始使用obj-c,虽然我可以在字符串中搜索简单的内容,但还没有达到可以搜索意外内容的地步。
我有一段格式化的文本,它总是以类似的方式出现。我不知道的是如何把它的某些部分拔出来。我可以创建一个string out if并找到某些片段,但我不确定这是否是正确的路径。我甚至不确定创建字符串是否是最好的方法。
我现在正在搜索的是大盲点,它是下面文本中的最后一个数字。它不会总是在一个统一的角色位置,因为球员的名字各不相同。它也不会总是最后的,还有一些我没有张贴的尾随文本。
我可以搜索“大盲”,然后移动范围来找到数字,但这个数字并不总是一致的,这就是我不知道如何考虑的。我现在正在使用subStringWithRange查找我想要的东西。
我可以得到第一个数字的索引,但是我怎么才能允许2-4个数字呢?如果我现在将它设置为4,它会给我一个超出范围的错误。
Full Tilt Poker Game #27219594154: $1 + $0.20 Sit & Go (Turbo) (211520821), Table 1 -
20/40 - No Limit Hold'em - 18:52:12 ET - 2011/01/12
Seat 1: Twigee (1,485)
Seat 2: tsaf21 (1,508)
Seat 3: dddewdddew (1,385)
Seat 4: MagMan1 (1,770)
Seat 5: Clyde the Fish (1,430)
Seat 6: Lazaro16 (1,340)
Seat 7: ascualliin (1,285)
Seat 8: aipeter (1,730)
Seat 9: AlligatorLega (1,567)
aipeter posts the small blind of 20
AlligatorLega posts the big blind of 40我真的很感谢你的帮助!到目前为止,Overflow的每个人都很棒!
谢谢
格雷厄姆
发布于 2011-01-28 12:04:52
我不确定我是否完全清楚这里的目标,但解决您发布的特定问题的一种方法是将字符串拆分成子字符串,这使得NSString类变得相当容易。下面是一个例子:
NSString *text = // Create the text string however you're currently creating it.
// Break the string into an array of strings, one for each line.
NSArray *lines = [text componentsSeparatedByString:@"\n"];
// Get the last line.
NSString *lastLine = [lines lastObject];
// Break the line into an array of words.
NSArray *words = [lastLine componentsSeparatedByString:@" "];
// Get the last word.
NSString *lastWord = [words lastObject];
// If you need to convert the string to a numeric type...
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:lastWord];
NSLog(@"%@", number);另一种方法是使用NSScanner实例来扫描您感兴趣的文本片段。这有点复杂,需要对C和Objective-C有更多的理解,但这里是:
// Create an instance of NSScanner containing the text
NSScanner *scanner = [NSScanner scannerWithString:text];
// Scan up to the words preceding the text you're interested in.
[scanner scanUpToString:@"big blind of " intoString:NULL];
// Skip past that part.
[scanner scanString:@"big blind of " intoString:NULL];
NSString *numericString;
// Scan the next substring. Here I'm assuming it's at the end of a line,
// so we're scanning until the newline character. Note that the numeric
// string will be created during the call to this method.
[scanner scanUpToString:@"\n" intoString:&numericString];
NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:numericString];
NSLog(@"%@", number);编辑
顺便说一句,您可以使用NSPredicate的一个实例来过滤线数组,如下所示:
// Break the string into an array of strings, one for each line.
NSArray *lines = [text componentsSeparatedByString:@"\n"];
// Filter the array of lines down to only those containing a given substring.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'big blind'"];
NSArray *matchingLines = [lines filteredArrayUsingPredicate:predicate];
// Make sure that the array is not nil or empty.
if ([matchingLines count] > 0)
{
// Get the first item in the array of matching lines.
NSString *firstMatch = [matchingLines objectAtIndex:0];
// Now break up the line up into words as before, or use an
// instance of NSScanner to scan the substring based on its position
// relative to other substrings.
NSArray *words = [firstMatch componentsSeparatedByString:@" "];
NSString *lastWord = [words lastObject];
NSLog(@"%@", lastWord);
}发布于 2011-01-28 12:24:10
尽管您可以使用原生NSString方法来扫描字符串、使用子字符串等等,但是这看起来确实是一个使用正则表达式的好地方。看起来您要对这些数据进行大量的解析,因此扫描/子串的工作将是一项巨大的痛苦。下面是一个正则表达式,它只匹配您正在讨论的行,并为您提供了两个捕获:
([\w]+[^\w]*?) posts the big blind of ([\d]*)第一个捕获是发布大盲的人的用户名(我知道你没有要求,但我打赌它会有帮助),第二个捕获是号码。较新版本的iOS包含使用正则表达式的NSRegularExpression类,但如果您需要支持较旧的软件,我强烈建议在Mac和iOS上使用RegexKitLite。
https://stackoverflow.com/questions/4824435
复制相似问题