このようなハッシュタグを検出できます。&hastagを含むハッシュタグを検出する
+ (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-certain-other-string – raki