2014-03-14 1 views
0

:아이폰 OS NSRegularExpression (. *) HEAD를 TAIL 예

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"hello: (.*)ABC" options:0 error:NULL]; 
NSString *str = @"hello: bobABC123ABC"; 
NSTextCheckingResult *match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])]; 
NSLog(@"macthing part is %@", [str substringWithRange:[match rangeAtIndex:0]]); 

매칭의 결과가 "bobABC123ABC"이므로, "ABC"의 정합 NSRegularExpression에서 첫 번째 대신 문자열에서 마지막 "ABC"를 찾습니다. 일치를 "밥"으로 지정하고 싶다면, 어느 누구도이를 달성하는 방법을 알고 있습니까?

+0

_bob_ 또는 _bobABC_? –

+0

마지막 "ABC"대신 첫 번째 "ABC"와 일치 –

답변

1

욕심을 느끼지 않는 정규 표현식을 만들어보세요. 말 :

@"hello: (.*?)ABC" 
      ^
      |==> note this 

대신 documentation에서

@"hello: (.*)ABC" 

의 :

*? 0 번 이상 매치하십시오. 가능한 한 몇 번 일치하십시오.

+0

@devnull, 감사합니다! –