开发者

newline replacement in string using regex

I'am using the following regex to remove newlines from a string:

$description =~ s/\r//;
$descripti开发者_开发问答on =~ 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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜