나는 NSLog
이 있는지 확인하기 위해 NSString
을 확인한 다음 주석 처리합니다. NSRegularExpression
을 사용하고 결과를 루핑합니다. 코드 :목표 C - 특정 하위 문자열이있는 NSRegularExpression
-(NSString*)commentNSLogFromLine:(NSString*)lineStr {
NSString *regexStr [email protected]"NSLog\\(.*\\)[\\s]*\\;";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *arrayOfAllMatches = [regex matchesInString:lineStr options:0 range:NSMakeRange(0, [lineStr length])];
NSMutableString *mutStr = [[NSMutableString alloc]initWithString:lineStr];
for (NSTextCheckingResult *textCheck in arrayOfAllMatches) {
if (textCheck) {
NSRange matchRange = [textCheck range];
NSString *strToReplace = [lineStr substringWithRange:matchRange];
NSString *commentedStr = [NSString stringWithFormat:@"/*%@*/",[lineStr substringWithRange:matchRange]];
[mutStr replaceOccurrencesOfString:strToReplace withString:commentedStr options:NSCaseInsensitiveSearch range:matchRange];
NSRange rOriginal = [mutStr rangeOfString:@"NSLog("];
if (NSNotFound != rOriginal.location) {
[mutStr replaceOccurrencesOfString:@"NSLog(" withString:@"DSLog(" options:NSCaseInsensitiveSearch range:rOriginal];
}
}
}
return [NSString stringWithString:mutStr];
}
문제는 테스트 케이스이다 : 대신 반환 "/*DSLog(@"A string");*/ /*DSLog(@"A string2")*/"
를 반환
NSString *str = @"NSLog(@"A string"); NSLog(@"A string2")"
: "/*DSLog(@"A string"); NSLog(@"A string2")*/"
.
문제는 Objective-C
이 정규식을 처리하는 방법입니다. 나는 arrayOfAllMatches
에 2 개의 결과를 기대할 것이지만 대신에 나는 하나만 얻는다. 에게 );
의 첫 번째 발생을 중지하도록 요청할 수있는 방법이 있습니까?