2011-11-06 2 views

답변

0

그것은 내 솔루션입니다 ""텍스트에 대한 enter image description here

정규 표현식 결과 ((\/\/).*(\n))이다. 나는 그것이 더 좋다는 것을 안다. 나에 대해 알고 위로 참조하지만 난 그것에 경험이 없어.

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