您好,我遇到了一个复杂的正则表达式问题。
mainString = @"Main Term (Rounded) [Square] ~a~d~j~."
我需要像这样返回
modifiedString* = @"Main Term (Rounded) [Square] adj."
所以 ~
之后的每个字符都必须是斜体或任何属性。
我需要修改后的 "adj."
范围,以便我可以添加属性。
谢谢。
NSError *error3 = nil;
NSRegularExpression *SHRegex = [NSRegularExpression regularExpressionWithPattern:@"\\~(.|)" options:0 error:&error3];
NSArray *matches3 = [SHRegex matchesInString:mainString options:0 range:NSMakeRange(0, [mainString length])];
NSUInteger *numberOfMatches = [SHRegex numberOfMatchesInString:mainString options:0 range:NSMakeRange(0, mainString.length)];
NSString *modifiedString = [SHRegex stringByReplacingMatchesInString:mainString options:0 range:NSMakeRange(0, [string length]) withTemplate:@""];
for (NSTextCheckingResult *match in matches3) {
NSRange matchRange = [match range];
NSRange firstHalfRange = [match rangeAtIndex:1];
//NSRange secondHalfRange = [match rangeAtIndex:2];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:0 blue:0 alpha:1] range:matchRange];
//[string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:firstHalfRange];
}
最佳答案
以下代码应该可以完成您想要的操作。它创建一个属性字符串 其中波浪号字符被删除,并且属性被添加到 波浪号后面的字符。我添加了一些评论,希望 解释它是如何工作的。
NSString *mainString = @"Main Term (Rounded) [Square] ~a~d~j~.";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:mainString];
NSError *error = nil;
// Pattern that matches a tilde followed by an arbitrary character:
NSString *pattern = @"(\\~)(.)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
__block NSUInteger offset = 0;
[regex enumerateMatchesInString:mainString options:0 range:NSMakeRange(0, [mainString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange firstHalfRange = [result rangeAtIndex:1]; // range of the tilde
NSRange secondHalfRange = [result rangeAtIndex:2]; // range of the following character
// Adjust locations according to the string modifications:
firstHalfRange.location += offset;
secondHalfRange.location += offset;
// Set attribute for the character:
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:1 green:0 blue:0 alpha:1] range:secondHalfRange];
// Remove the tilde:
[[attrString mutableString] deleteCharactersInRange:firstHalfRange];
// Update offset:
offset -= firstHalfRange.length;
}];
更新以响应您的评论:以下代码匹配两种模式(波形符或插入符号后跟一个字符)并使用不同的属性进行替换。
NSString *mainString = @" ~a^b~c^d";
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:mainString];
NSError *error = nil;
NSString *pattern = @"(\\~|\\^)(.)";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:&error];
__block NSUInteger offset = 0;
[regex enumerateMatchesInString:mainString options:0 range:NSMakeRange(0, [mainString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange firstHalfRange = [result rangeAtIndex:1];
NSRange secondHalfRange = [result rangeAtIndex:2];
NSString *firstMatch = [mainString substringWithRange:firstHalfRange];
// Adjust locations according to the string modifications:
firstHalfRange.location += offset;
secondHalfRange.location += offset;
// Set color attribute for the character:
if ([firstMatch isEqualToString:@"~"]) {
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:1 green:0 blue:0 alpha:1] range:secondHalfRange];
} else {
[attrString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0 green:1 blue:0 alpha:1] range:secondHalfRange];
}
[[attrString mutableString] deleteCharactersInRange:firstHalfRange];
// Update offset:
offset -= firstHalfRange.length;
}];
关于ios - 复杂的正则表达式文本修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20830992/