SPUG: Removing newlines from a multi-line string

Jonathan Gardner gardner at sounddomain.com
Wed Mar 14 19:38:29 CST 2001

Let me correct myself... I feel like I am about two feet tall right now...

> You need the 's' option to make it a single line. Otherwise, you can MATCH the
> "\n" with $, but you can't replace it.
If you use /\n/ you will match the newline no matter what. But if you use /./,
you will never match the newline. That is why they have the /s option. For some
reason I got it stuck in my head that when you want to treat \n as data rather
than a logical seperator you should use /s. 

> The 'o' option is good because it will compile the regex exactly once, rather
> than recompiling it each time it encounters it.
Only when the regex has a variable inside. Otherwise, it is meaningless, and
has no effect. Perl will compile regexes that have no variables inside
once and only once, even if there is no /o. Sorry, my bad. Another habit of

So the difference between s/[\r\n]//sog and s/[\r\n]//g is that one is easier to
type by two characters, and is thus the better answer because laziness is a
virtue in Perl.

Do I get extra points for being lazy and not looking anything up before I post?

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