SPUG: Removing newlines from a multi-line string

Jonathan Gardner gardner at sounddomain.com
Wed Mar 14 11:24:40 CST 2001


On Wed, 14 Mar 2001, David Vergin wrote:
> If you are on Windows, you are dealing with two characters at the end of each
> line: \r and \n.
> (And you don't need /i (case-insensitive matches)). So try:
> 
>     $html =~ s/[\n\r]//g;
> 
> dv
> 
> >         $html =~ s/\n//ig;
> >
> > Josh

You need the 's' option to make it a single line. Otherwise, you can MATCH the
"\n" with $, but you can't replace it.

$html =~ s/[\r\n]//sog;

The 'o' option is good because it will compile the regex exactly once, rather
than recompiling it each time it encounters it.

It is good to remember the "\r" because windows sometimes submits a "\r" with
each newline from a text box. (Like dv mentioned)

The 'i' option is unnecessary unless you are using alphabetic characters. In
this case, there is no such thing as a lower case "\n" so it is useless.

-- 
Jonathan Gardner
gardner at sounddomain.com
(425)820-2244 x123


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
  Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/





More information about the spug-list mailing list