嗨,我有某种复杂的正则表达式情况。
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/