0

私がこのテキストを持っているとしましょう:

word1 word2 " word3 //" word4

コメント用の正規表現を書く必要があります。私の解決策は今です((\/\/).*(\n))ここに画像の説明を入力してください

次の""のテキストの正規表現((\").*(\"))

4

1 に答える 1

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

私のブログで答える

于 2011-11-08T00:17:54.447 に答える