이렇게 해시 태그를 감지 할 수 있습니다.해시 태그를 포함한 해시 태그 검색
+ (NSArray *)getHashArrayWithInputString:(NSString *)inputStr
{
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error];
NSArray *matches = [regex matchesInString:inputStr options:0 range:NSMakeRange(0, inputStr.length)];
NSMutableArray *muArr = [NSMutableArray array];
for (NSTextCheckingResult *match in matches) {
NSRange wordRange = [match rangeAtIndex:1];
NSString* word = [inputStr substringWithRange:wordRange];
NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
if ([word rangeOfCharacterFromSet:notDigits].location == NSNotFound)
{
// newString consists only of the digits 0 through 9
}
else
[muArr addObject:[NSString stringWithFormat:@"#%@",word]];
}
return muArr;
}
문제 inputStr은 "& #D D"인 경우에만 #D를 검출 할 수 있다는 것이다. 어떻게해야합니까?
도움이 될 수도 있습니다. http://stackoverflow.com/questions/7934688/how-to-see-if-an-nsstring-starts-with-a-other-other-string – raki