希望这可以帮助:
NSString *string = @"This is a sentence. It is marked by a period. This sentence is not marked by one How do you do? I'm doing very good!";
NSError *error = nil;
NSString *pattern = @"(\\.|,|!|\\?|\\n)\\s*";
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:pattern
options:0
error:&error];
if (expression)
{
NSArray *matches = [expression matchesInString:string
options:0
range:NSMakeRange(0, [string length])];
NSLog(@"%@", matches);
if ([matches count] > 0)
{
NSMutableArray *sentences = [[NSMutableArray alloc] initWithCapacity:[matches count]];
NSUInteger sentenceStart = 0;
for (NSTextCheckingResult *result in matches)
{
NSUInteger sentenceEnd = result.range.location + 1;
[sentences addObject:[string substringWithRange:NSMakeRange(sentenceStart, sentenceEnd - sentenceStart)]];
sentenceStart = sentenceEnd + (result.range.length - 1);
}
NSLog(@"%@", sentences);
}
}
else
{
NSLog(@"ERROR: %@", error);
}