2013-04-16 41 views
0

我无法找到具有平衡标签的正则表达式,一个用于打开,一个用于关闭。 标签必须包含多行文字;并且标记是动态的,在编译时未定义NSRegularexpression multiline and stack

我无法匹配与开始标记对应的结束标记,例如{{home}} - > {{/ home}}和{{hello}} - > {{/ hello}};它匹配{{家}}到{{/你好}} 任何帮助,将不胜感激

任何帮助,将不胜感激

葛瑞格尔

PS:我评论非工作正则表达式

NSString * string; 
    NSError* error = nil; 
    NSRegularExpression* regex; 
    NSArray* matches; 


    string = 
    @" The {{demo}}following tables describe the {{/demo}}character expressions" 
    " used by the regular expression to match patterns within " 
    " a string,{{home}} the pattern operators that specify how many" 
    " times a pattern is matched and additional matching" 
    " restrictions, and the last {{/home}}table specifies flags" 
    " that can be included in the regular "; 


    regex = [NSRegularExpression regularExpressionWithPattern: 
      @"\\{\\{demo\\}\\}" 
      "(.*)?" 
      "\\{\\{(//|/demo)\\}\\}" 
                 options:NSRegularExpressionDotMatchesLineSeparators 
                 error:&error]; 


// regex = [NSRegularExpression regularExpressionWithPattern: 
//    @"\\{\\{.*\\}\\}" 
//    "(.*)?" 
//    "\\{\\{(//|/.*)\\}\\}" 
//              options:NSRegularExpressionDotMatchesLineSeparators 
//              error:&error]; 


    matches = [regex matchesInString:string 
          options:NSRegularExpressionDotMatchesLineSeparators 
           range:NSMakeRange(0, [string length])]; 

    for (NSTextCheckingResult* match in matches) 
    { 
     NSLog(@"### %@ ###", [string substringWithRange:[match range]]); 
    } 

回答

0

您可以使用此模式

\{\{([^/]*?)\}\}(.*?)\{\{(/\1|//)\}\} 
    -------  ----  -------- 
     |   |   |->matches closing tag having value same as group 1 or // 
     |   |->content in group 2 
     |->matches the opening tag value and captures it in group1 
+0

感谢您的帮助,我不知道/发现群体的概念。除了我为多个用户做了一些修改之外,它运行良好:@“\\ {\\ {([^ /] *?)\\} \\}” “。*?” (这是变化) “\\ {\\ {((/ \\ 1)|(//))\\} \\}” –

+0

它的工作方式就像一个魅力,谢谢 –