使用正则表达式删除空行

Remove Empty lines using Regular Expression

我是 iOS 开发的新手,我有下面这样的数据,我想删除所有空行,请告诉我要应用的模式:

我已经将模式用作@"(\r\n)",并将其替换为@"",但它不起作用。请帮我解决这个问题

#EXTINF:-1 tvg-name="seedocs" tvg-logo="RT",RT





http://rt.ashttp14.visionip.tv/live/rt-global-live-HD/playlist.m3u8

#EXTINF:-1 tvg-name="hsn" tvg-logo="hsn",HSN TV

rtsp://hsn.mpl.miisolutions.net:1935/hsn-live01/_definst_/mp4:420p500kB31

#EXTINF:-1 tvg-name="us" tvg-logo="us",USTwit

http://bglive-a.bitgravity.com/twit/live/high

#EXTINF:-1 tvg-name="ALJAZEERA" tvg-logo="aljazeera",Aljazeera

rtmp://aljazeeraflashlivefs.fplive.net/aljazeeraflashlive-live/aljazeera_eng_high

#EXTINF:-1 tvg-name="bbc" tvg-logo="bbc",BBC World News



#EXTINF:-1 tvg-name="vevo" tvg-logo="vevo",Vevo

http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/06/prog_index.m3u8

#EXTINF:-1 tvg-name="vevo2" tvg-logo="vevo2",Vevo 2

http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch3/06/prog_index.m3u8

#EXTINF:-1 tvg-name="1HD" tvg-logo="1HD",1HD

rtmp://109.239.142.62/live/livestream3
[\r\n]+

您可以通过 \n 使用此 instead.See demo.Replace。

https://regex101.com/r/vV1wW6/26

NSString *string = @"Your multiline string";
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[\r\n]+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"\n"];

搜索:(?:\r?\n){2,} 替换:\r\n

\r?\n 与 Windows 和 Linux 兼容。 {2,} 表示 "two or more instances"

demo


如果支持,您可以使用 \R 而不是 \r?\n 来包含其他类型的 Unicode 换行符。这可能在将来有用,如果现在没有用的话。

可以使用NSString方法
stringByReplacingOccurrencesOfString:withString:options:range:
使用选项:
NSRegularExpressionSearch.

不需要使用NSRegularExpression

NSString *cleanText = [originalText stringByReplacingOccurrencesOfString:@"[\r\n]+" withString:@"\n" options:NSRegularExpressionSearch range:NSMakeRange(0, text.length)];

这将用单个 \n 替换 \r and/or \n 的任何组合的所有运行,从而消除所有银行行。