posted by dalnimbest 2013. 8. 4. 14:29

// 'strBodyAll' is a variable that has text you are dealing with ('strBodyAll'에서 여러줄을 한줄로 바꿀려고 한다.)



//  '\r' 은 뉴라인이고, '+'은 그앞의 표현('\r')이 한줄또는 그이상이 나타나는것을 말한다.
NSRegularExpression *regEx= [NSRegularExpression regularExpressionWithPattern:@"\r+" options:NSRegularExpressionCaseInsensitive error:nil];


// Replacing actually happens here with one new line('\r') (실제로 여기서 치환이 일어난다.)
[regEx replaceMatchesInString:strBodyAll options:NSRegularExpressionCaseInsensitive range:NSMakeRange(0, [strBodyAll length]) withTemplate:@"\r"];


//  =================Bonus==================

// If you want to how many replace will be happen, just use below code (만약 몇개가 치환되는지 알고 싶으면 아래 코드를 사용할것)


// 'numberOfMatches' variables has the number of matched multilple new lines in 'strBodyAll' ('numberOfMatches'변수는 'strBodyAll'에서 나타는 여러줄의 총 수를 말한다.

NSUInteger numberOfMatches = [regEx numberOfMatchesInString:strBodyAll
                                                    options:0
                                                      range:NSMakeRange(0, [strBodyAll length])];