问题
I'm using a regular expression in a parser, however, it seems to give one result to much, this is my code: Regex:
self.seatSelectRegex = [NSRegularExpression regularExpressionWithPattern:@"Seat ([0-9]{1,2}): (.*) \\([$£€]?([0-9.]+) in chips\\).*$" options:NSRegularExpressionAnchorsMatchLines error:&error];
Code:
NSMutableDictionary *players = [[NSMutableDictionary alloc] init];
[self.seatSelectRegex enumerateMatchesInString:input options:NSMatchingCompleted range:NSMakeRange(0, input.length) usingBlock:
^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop)
{
NSLog(@"%lu", result.range.length);
Player *p = [[Player alloc] init];
p.name = [input substringWithRange:[result rangeAtIndex:2]];
p.seatNumber = [input substringWithRange:[result rangeAtIndex:1]].intValue;
p.stack = [input substringWithRange:[result rangeAtIndex:3]].doubleValue;
[players setValue:p forKey:p.name];
}];
I'm expecting 3 results with my input, however, I get 4, where the last result has a range with location = 0 and length = 0 (the first three are all correct). Is this common behavior and should I just check the location and length of the range, or is there an error somewhere?
For what it's worth, this is my input:
PokerStars Hand #81669312371: Hold'em No Limit ($0.01/$0.02 USD) - 2012/06/08 16:57:33 CET [2012/06/08 10:57:33 ET]
Table 'Icarus III' 6-max Seat #2 is the button
Seat 2: SanderDecler ($2 in chips)
Seat 3: ehrli87 ($0.90 in chips)
Seat 4: umar.11 ($1.60 in chips)
ehrli87: posts small blind $0.01
umar.11: posts big blind $0.02
*** HOLE CARDS ***
Dealt to SanderDecler [Kh 7d]
SanderDecler: raises $0.04 to $0.06
ehrli87: folds
umar.11: calls $0.04
*** FLOP *** [Jc Tc Jh]
umar.11: checks
SanderDecler: bets $0.08
umar.11: raises $0.24 to $0.32
SanderDecler: folds
Uncalled bet ($0.24) returned to umar.11
umar.11 collected $0.28 from pot
*** SUMMARY ***
Total pot $0.29 | Rake $0.01
Board [Jc Tc Jh]
Seat 2: SanderDecler (button) folded on the Flop
Seat 3: ehrli87 (small blind) folded before Flop
Seat 4: umar.11 (big blind) collected ($0.28)
回答1:
This is because your using the NSMatchingReportCompletion
option with enumerateMatchesInString:options:range:usingBlock:
. From Apples documentation:
If the NSMatchingReportCompletion matching option is specified, the Block object will be called once after matching is complete, with nil result and the NSMatchingCompleted matching flag is set in the flags passed to the Block, plus any additional relevant “NSMatchingFlags” from among NSMatchingHitEnd, NSMatchingRequiredEnd, or NSMatchingInternalError.
And the reason your seeing the last block call as a range with location and length set as 0 is because your sending messages to nil
which will return nil
(which is the integer 0).
来源:https://stackoverflow.com/questions/10961779/nsregularexpression-enumeratematchesinstringoptionsrangeusingblock-giving-a