in reply to Regular expressions CR/Linefeeds

By the words 'that will detect', do you actually need to know which combination of CR/LF you have been passed in some input? Because if all you really need to do is delete these characters from a string of text, then you can simply use one of the following lines:

$text =~ tr/\r\n//d; # using tr/// $text =~ s/[\r\n]+//g; # using s///

I'm not sure if the following could actually be useful or not, but who knows? I'll post it and you can do whatever you please with it. I only included the data you presented, so it's not setup for things like mac detection.

# Contains a linefeed if ($text =~ /\n/) { # Also contains a carriage return if ($text =~ /\r/) { print "Seems like DOS text.\n"; } else { print "Seems like unix text.\n"; } } else { print "No linefeeds detected in the text.\n"; }

Replies are listed 'Best First'.
Re: Re: Regular expressions CR/Linefeeds
by rob_au (Abbot) on Jan 22, 2003 at 08:35 UTC
    There is a section in perlport concerning the handling of line feeds across different platforms - Unix traditionally uses \012, one kind of Windows I/O uses \015\012, and Mac OS uses \015. The recommended pattern for matching given in this documentation is \015?\012 given the differences in the interpretation of \n by STDIO (or PerlIO depending upon the version and build of Perl).

     

    perl -le 'print+unpack("N",pack("B32","00000000000000000000001000100011"))'