I have this text:
1
|
word1 word2 " word3 //" word4
|
I wrote simple solution. I know it can be better. I know about Back Reference, but i don’t have experience with it.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
NSRegularExpression *expression = [NSRegularExpression regularExpressionWithPattern:@"((@\"|\").*?(\"))"
options:NSRegularExpressionDotMatchesLineSeparators error:nil]; NSArray *textArray = [expression matchesInString:textString options:0 range:NSMakeRange(0, [textString length])]; for (NSTextCheckingResult *result in textArray) { // set color for range } // Comments expression = [NSRegularExpression regularExpressionWithPattern:@"(//[^\"\n]*)" options:0 error:nil]; NSArray * arrayComments = [expression matchesInString:textString options:0 range:NSMakeRange(0, [textString length])]; for (NSTextCheckingResult *resultComment in arrayComments) { BOOL inside = NO; for (NSTextCheckingResult *resultText in textArray) { 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 } } |