我有一个场景,我有一个看起来像 -
的字符串"Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n\nLorem Ipsum has been the industry's \n\nstandard dummy text ever since the 1500s.when an unknown printer took a galley of type and scrambled it to make a type specimen book."
我想检索行"Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
并将其设为斜体。所以我将完整的句子嵌入如下的方括号中并创建一个正则表达式,并执行如下操作 -
我不想手动计算我这样做的范围。
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. \n\n[Lorem Ipsum has been the industry's \n\nstandard dummy text ever since the 1500s].when an unknown printer took a galley of type and scrambled it to make a type specimen book."
并编写以下代码以便检索 -
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\[(.*?)\\]" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *myArray = [regex matchesInString:testString options:0 range:NSMakeRange(0, [testString length])] ;//testString is my concerned String
NSMutableArray *matches = [NSMutableArray arrayWithCapacity:[myArray count]];
for (NSTextCheckingResult *match in myArray) {
NSRange matchRange = [match rangeAtIndex:1];
[matches addObject:[testString substringWithRange:matchRange]];
NSLog(@"%@", [matches lastObject]);
}
现在,如果我没有使用任何新的行语法(即当我删除\ n时),我能够检索该块。但当\ n存在时,它不会检索相同的内容。
那么可能是什么问题。