regex - Perl - can't strip blank lines -
let's say, have file (it's not actual content hexdump):
0000000 \r \n \r \n t h s s f 0000010 l e \r \n \r \n h e r 0000020 e ' s s o m e t e x t \r \n 000002f
if run following:
#!/usr/bin/perl use strict; use warnings; use file::slurp; $_ = read_file("file.txt"); s/^\s*$//mg; print;
the output produced is:
0000000 \n t h s s f l e \r 0000010 \n \n h e r e ' s s o m e t e 0000020 x t \r \n
apparently, blank lines aren't stripped.
can point out i'm doing wrong?
in regexes, $
assertion can bit confusing. according docs, “match[es] end of line (or before newline @ end)”. behaves like
(?=\n\z)|\z
with /m
modifier, changes to
(?=\n)|\z
this means \n
not included in matched substring. want:
s/^\s*\n//mg;
now there remain points in code should addressed. mainly, makes little sense read in whole file @ once, , run regex on it. rather, i'd do:
use strict; use warnings; use autodie; open $fh, "<", "file.txt"; while (<$fh>) { print if /\s/; # print if line contains @ least 1 non-space character # elegantly skips whitespace-only lines. }
this assumes line endings consist entirely of whitespace characters , end \n
. holds both \r\n
, \n
line endings. else, assign custom line endings like
local $/ = local $\ = "\r\n"; # input , output line endings while (<$fh>) { chomp; # remove line endings print if /\s/; # print adds line ending again. }
Comments
Post a Comment