2015-10-15 46 views
0

我得到一個帶有標籤的HTML格式化字符串。但「<br>」標籤存在一個不穩定的情況。我以隨機格式獲得「<br>」標籤,這會在文本中產生一些額外的空間。那麼是否有解決方案來修剪這些多個「<br>」標籤?文字HTML中的正則表達式Objective C

例子: -

"some text..... <br>" 
"some text..... <br> <br>" // <-- this cause some extra space. 
"some text..... <br>\n" // <-- this cause some extra space. 
"some text..... <br>\n<br>" // <-- this cause some extra space. 
"some text..... <br><br>\n  <br>" // <-- this cause some extra space. 

因此,在短期,可以有白色的空間,並在其中\n字符多個「<br>」標籤。

我需要用單個「<br>」標記替換它。任何人都可以用同樣的正則表達式來幫助我嗎?

注意: -我不想替換兩個"<br>"標記之間的任何其他字符串,除了空格和「\ n」。寫至今

碼 -

NSMutableString *temp = [[NSMutableString alloc] initWithString:html]; 

      [temp replaceOccurrencesOfString:@"<br><br>" withString:@"<br>" options:0 range:NSMakeRange(0, [temp length])]; 
      [temp replaceOccurrencesOfString:@"<br>\n<br>" withString:@"<br>" options:0 range:NSMakeRange(0, [temp length])]; 
      [temp replaceOccurrencesOfString:@"<br>\n" withString:@"<br>" options:0 range:NSMakeRange(0, [temp length])]; 

它是一個靜態的字符串,需要regex它。

+0

一個選項可以得到兩個''
標籤之間的字符串和修剪與'\ N'和'space'的字符串。如果修剪過的字符串爲空,則可以將其完全替換。 – sargeras

回答