代码之家  ›  专栏  ›  技术社区  ›  joe

使用正则表达式删除空行

  •  0
  • joe  · 技术社区  · 9 年前

    我是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
    
    3 回复  |  直到 9 年前
        1
  •  4
  •   abintom    9 年前
    [\r\n]+
    

    您可以改用此选项。请参见演示。替换为 \n .

    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"];
    
        2
  •  1
  •   Eirik Birkeland    9 年前

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

    \r?\n 同时兼容Windows和Linux。 {2,} 指“两个或多个实例”

    demo


    如果支持,您可以使用 \R 而不是 \r?\n个 以包括其他类型的Unicode换行符。这可能在未来有用,如果不是目前。

        3
  •  0
  •   zaph    9 年前

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

    无需使用 NSRegularExpression .

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

    这将替换以下任意组合的所有运行 \r 和/或 \n 有一张单人床 \n个 从而消除所有银行行。