0

I am trying to substitute any \n\ character with whitespace, but somehow \s isn't recognised as a whitespace substitution character.

$match_to_array =~ s/\n/\s/;

iridescent
  • 305
  • 4
  • 14
  • Similar question/answer (Java): [Replace new line/return with space using regex](http://stackoverflow.com/a/11049108/626273) – stema May 08 '13 at 07:41

1 Answers1

3

\s is a whole class of characters. It can mean , \t, \r, \n, or \f. You have to tell Perl which one to use. For example, space:

$match_to_array =~ s/\n/ /
                       ^^^

Or tab:

$match_to_array =~ s/\n/\t/
                       ^^^^
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • Thanks. I didn't know whitespace could mean more than just a 'space'. Cheers. – iridescent May 06 '13 at 14:04
  • 1
    `\s` is only a regex character class that matches tab, linefeed, formfeed, carriage-return and space. In a double-quoted string it just means `s`, and the replacement part of a substitution is just a double-quoted string. – Borodin May 06 '13 at 15:37