Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 复杂正则表达式文本修改_Ios_Regex_Nsstring_Nsregularexpression - Fatal编程技术网

Ios 复杂正则表达式文本修改

Ios 复杂正则表达式文本修改,ios,regex,nsstring,nsregularexpression,Ios,Regex,Nsstring,Nsregularexpression,嗨,我有一个复杂的正则表达式情况 mainString = @"Main Term (Rounded) [Square] ~a~d~j~." 我需要像你一样把这个还给你 modifiedString* = @"Main Term (Rounded) [Square] adj." 所以~之后的每个字符必须是斜体或任何属性。 修改后,我需要“adj.”的范围,以便添加属性 多谢各位 NSError *error3 = nil; NSRegularExpression *SHRegex

嗨,我有一个复杂的正则表达式情况

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;
}];

只是想澄清一下:是否要从字符串中删除所有
~
字符,并返回这些字符所包围的所有字符的位置?它是否总是
~a~b~c~d~
,或者可能是
~abc~d~e~
——在后一种情况下,您希望返回
abc d e
“特殊匹配字符”?你有
~a~d~j~。
并希望
adj.
是“特殊的”。这是否意味着您只需要在特殊字符之前添加一个波浪号?它可以是
Main~a~d~j~。Sub~a~d~v~。
,是的,我想删除
,并返回剩余
adj.
Main
Sub
的范围或位置。非常感谢。这就是我需要的。我的数据基本结构在每一个应该是下标的字符之前都有一个平铺。多亏了你。但我也希望
^
后面的每一个字符都是上标的。添加相同的代码就可以了,但是如果同一个条目同时有两个变量,它就会崩溃为
IndexOutfrange
。有什么建议吗?