ParserKit을 이용하여 주어진 문자열을 토큰별로 분리 시키는 예제
먼저 문자열을 토큰별로 분리하여 그것에 대한 Type을 출력하는 예제이다.
- (void) parserString{
NSString *s=@"2 !=sl/*한국어skd*/ bal @#WWW// comm";
PKTokenizer *t = [PKTokenizer tokenizerWithString:s];
PKToken *eof = [PKToken EOFToken];
PKToken *tok = nil;
while(( tok = [t nextToken]) != eof){
NSLog(@"(%@) (%.f) : %@",
tok.stringValue, tok.floatValue, [tok debugDescription]);
}
}
그래서 결과는

특정 토큰에 대해서 인식의 설정을 하는 방법
Float point의 "."을 Symbol로 인식하지 않고 Float으로 인식하게
t.numberState.allowsTrailingDot = YES;
과학에서 사용하는 exponential number를 인식하게 하는 설정
t.numberState.allowsScentificNotation = YES;
«020» (octal 16) and «0x20» (hex 32)와 같은것을 인식하게 하는 설정
t.numberState.allowsOctalNotation = YES;
t.numberState.allowsHexadecimalNotation = YES;
이번에는 특정 영역에서 발생하는 토큰을 해당 토큰Type으로 인식하게
[t setTokenizerState:t.wordState from:'0' to:'9']; 0 ~ 9를 word로 인식
[t setTokenizerState:t.quoteState from:'#' to:'#']; # ~ #를 인용문으로 인식
[t setTokenizerState:t.numberState from:'+' to:'+']; + ~ +를 숫자로 인식
[t setTokenizerState:t.commentState from:'/' to:'/'];
[t.commentState addSingleLineStartSymbol:@"//"];
[t.commentState addMultiLineStartSymbol:@"/*" endSymbol:@"*/"];
/ ~ /, //. /* ~ */ 를 코멘트로 인식
[t setTokenizerState:t.commentState from:'#' to:'#'];
[t.commentState addSingleLineStartSymbol:@"#"];
#를 코멘트로 인식
[t setTokenizerState:t.commentState from:'<' to:'<'];
[t.commentState addMultiLineStartSymbol:@"<!--" endSymbol:@"-->"];
XML, HTML에서 코멘트로 인식
'개발 > Objective-C' 카테고리의 다른 글
[Objective-C] String Function in Object-C (0) | 2010.08.29 |
---|---|
[Objectiv-C] Google Code에 있는 ParseKit을 연결하기 (0) | 2010.08.29 |
[Objectiv-C] UILabel과 관련된 주요 설정값 (0) | 2010.08.29 |
[Objectiv-C] Collection에 대해서 (0) | 2010.08.29 |
[Objective-C] Memory 관리 (0) | 2010.08.29 |