2012-06-09 5 views
5

sto usando un'espressione regolare in un parser, tuttavia, sembra dare un risultato molto, questo è il mio codice: Regex:NSRegularExpression enumerateMatchesInString: options: range: usingBlock: fornisce un risultato nullo?

self.seatSelectRegex = [NSRegularExpression regularExpressionWithPattern:@"Seat ([0-9]{1,2}): (.*) \\([$£€]?([0-9.]+) in chips\\).*$" options:NSRegularExpressionAnchorsMatchLines error:&error]; 

Codice:

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' Mi aspettavo 3 risultati con il mio input, tuttavia, ottengo 4, dove l'ultimo risultato ha un intervallo con posizione = 0 e lunghezza = 0 (i primi tre sono tutti corretti). È questo comportamento comune e dovrei semplicemente controllare la posizione e la lunghezza dell'intervallo, oppure c'è un errore da qualche parte?

Per quel che vale, questo è il mio ingresso:

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) 
+0

FWIW, la tua regex sembra funzionare correttamente (ho ottenuto 3 partite in RegexBuddy, come previsto). –

+0

Sì, ho avuto lo stesso problema con qualche altro regex, ma poiché avevo solo bisogno di 1 risultato, ho usato solo il firstMatchInString: options: range: function ... –

risposta

7

Questo perché il utilizzando l'opzione NSMatchingReportCompletion con enumerateMatchesInString:options:range:usingBlock:. Da Apples documentation:

Se viene specificata l'opzione corrispondente NSMatchingReportCompletion, l'oggetto di blocco sarà chiamata una volta dopo di corrispondenza è completa, con esito nullo e il corrispettivo bandiera NSMatchingCompleted è impostato nelle bandiere passati alla Block, aumentato degli eventuali rilevanti "NSMatchingFlags" tra NSMatchingHitEnd, NSMatchingRequiredEnd o NSMatchingInternalError.

E il motivo per il vostro visto l'ultima chiamata blocco come una serie con la posizione e la durata impostata come 0 è perché il vostro invio di messaggi a nil che restituirà nil (che è il numero intero 0).

+0

Grazie! Era così. –

+0

Nessun problema. Non sapevo di questa opzione quindi grazie per la domanda :) –

Problemi correlati