Need help formatting text / search a string for a varrying size number
I'm just starting out with obj-c and while I can search for simple things in a string I'm not to the point where I can search for un-expected thing开发者_运维技巧s.
I've got a piece of formatted text that will always appear in a similar fashion to this. What I don't know is how to pull certain pieces of it out. I can create a string out if and find certain pieces but I'm not sure if that's the right path to be taking. I'm not even sure if creating a string is the best way either.
What I'm searching for right now is the big blind, it's the very last number in the text below. It won't always be at a uniform character location because of the varying player names. It won't always be last either, there is trailing text that i didn't post.
I can search for "big blind" then shift the range to find the numbers but that number won't always be uniform and that is what I don't know how to allow for. I'm using subStringWithRange right now to find what I want.
I can get the index of the first number but how do I allow for 2-4 total digits? If I set it to 4 right now it would give me an out of range error.
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
I really appreciate the help! Everyone at Overflow has been awesome so far!
Thanks
Graham
I'm not sure I'm completely clear on the goal here, but one way to attack the specific problem you posted would be to break the string into substrings, which the NSString class makes fairly easy. Here's an example:
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);
Another approach would be to use an instance of NSScanner
to scan the bits of text you're interested in. That's a bit more complex, and takes a little bit more understanding of C and Objective-C, but here goes:
// 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);
EDIT
By the way, you could filter the array of lines using an instance of NSPredicate as follows:
// 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);
}
Though you can use the native NSString methods to scan through the string, use substrings, and all of that jazz, this really seems like a good place to use regular expressions. It seems like you're going to be doing a lot of parsing of this data, and because of that scanning/substring work is going to be a gigantic pain. Here is a regular expression that matches only the line you are talking about, and gives you two captures:
([\w]+[^\w]*?) posts the big blind of ([\d]*)
The first capture is the username of the person that posted the big blind (I know you didn't ask for that, but I bet it'll be helpful), and the second capture is the number. Newer versions of iOS contain the NSRegularExpression
class to use regexes, but if you need to support older software, I highly recommend RegexKitLite for both Mac and iOS.
精彩评论