2011-11-06 79 views

回答

0

這是我的解決方案。我知道它可以更好。我知道返回參考但我沒有經驗。

NSRegularExpression *exp = [NSRegularExpression regularExpressionWithPattern:@"((@\"|\").*?(\"))" 
              options:NSRegularExpressionDotMatchesLineSeparators 
              error:nil]; 
NSArray *textArr = [exp matchesInString:string options:0 range:NSMakeRange(0, [string length])]; 

for (NSTextCheckingResult *result in textArr) { 
    // set color for range 
} 


// Comments 
exp = [NSRegularExpression regularExpressionWithPattern:@"(//[^\"\n]*)" 
               options:0 
               error:nil]; 

NSArray * arrayComments = [exp matchesInString:string options:0 range:NSMakeRange(0, [string length])]; 

for (NSTextCheckingResult *resultComment in arrayComments) { 

    BOOL inside = NO; 
    for (NSTextCheckingResult *resultText in textArr) { 
     NSInteger from = resultText.range.location; 
     NSInteger to = resultText.range.location+resultText.range.length; 
     NSInteger now = resultComment.range.location; 
     if (from < now && now < to) { 
      inside = YES; 
      break; 
     } 
    } 
    if (!inside) { 
     // set color for range 
    } 
} 

answer on my blog