Replacing a newline in a line using regex

I use the following regex to remove string lines from a string:

$description =~ s/\r//; $description =~ s/\n//; 

but then I believe in:

 $description =~ m/\n/ 

It seems regex has not replaced all the lines of a newline from a line, any help with this?

+4
source share
2 answers

Your substitutions are not global substitutions - they replace only the first instance of the template in a string. To make a global replacement, add g after the last slash, for example:

 $description =~ s/\r//g; $description =~ s/\n//g; 

You can also combine two substitutions into one substitution using a character set:

 $description =~ s/[\n\r]//g; 
+4
source

If you are trying to delete individual characters, use tr , not s/// .

 $description =~ tr/\r\n//d; 

This will delete all occurrences of either \r or \n , regardless of their respective positions in the line.

+9
source

All Articles