I'am using the following regex to remove newlines from a string:
$description =~ s/\r//;
$description =~ s/\n//;
but afterwards I am getting true for:
$description =~ m/\n/
It seems regex did not replace all newlines from the string, any help with this?
If you're trying to remove single chars, use tr
rather than s///
.
$description =~ tr/\r\n//d;
This will remove all occurrences of either \r
or \n
regardless of their respective positions in the string.
Your substitutions are not global substitutions - they replace only the first instance of the pattern in the string. To make a global substitution, add a g after the final slash, like so:
$description =~ s/\r//g;
$description =~ s/\n//g;
You can also combine the two substitutions into a single substitution using a character set:
$description =~ s/[\n\r]//g;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With