ios - 复杂的正则表达式文本修改

标签 ios regex nsstring nsregularexpression

您好,我遇到了一个复杂的正则表达式问题。

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/

相关文章:

没有 Storyboard的 iOS 应用程序(大小类别和设备类型限制)

iOS Xcode 10 模拟器错误(OSStatus 错误 -600)

html - 我想在 jmeter 中忽略带有正则表达式的空格

python - 使用 Python 分隔最后出现的数字的正则表达式

ios - 如何在 iOS 中使用 Google map API 在 mapView 上获取触摸位置的纬度和经度

ios - 在 iOS 应用程序中存储经过身份验证的用户的访问 token 的最合适位置在哪里?

php - 在 phpstorm 中使用正则表达式使用替换工具删除 php 注释

objective-c - 比较 Cocoa 中的字符串

objective-c - 将属性添加到代表 "to the power of.."的 nsstring

ios - 无法在多行 nsstring 上获得投影