Neatest way to remove linebreaks in Perl

前端 未结 7 1165
孤街浪徒
孤街浪徒 2020-12-23 09:16

I\'m maintaining a script that can get its input from various sources, and works on it per line. Depending on the actual source used, linebreaks might be Unix-style, Windows

相关标签:
7条回答
  • 2020-12-23 09:57

    After digging a bit through the perlre docs a bit, I'll present my best suggestion so far that seems to work pretty good. Perl 5.10 added the \R character class as a generalized linebreak:

    $line =~ s/\R//g;
    

    It's the same as:

    (?>\x0D\x0A?|[\x0A-\x0C\x85\x{2028}\x{2029}])
    

    I'll keep this question open a while yet, just to see if there's more nifty ways waiting to be suggested.

    0 讨论(0)
提交回复
热议问题