开发者

How to check for | in doing substitution in Perl

I have a string with the following data in it

"email@domain.com | firstname | lastname"

I want to replace | lastname with a different value. If I use s/ to do a substitution, what do i need to do to the | to get it to recognize. If开发者_开发知识库 I do

$foo =~ s/ lastname/fillertext/;

it works fine. but if I do

$foo =~ s/ | lastname/fillertext/;

it doesn't work. I tried to do - \|/ lastname, "| lastname", '| lastname'.


| has a special meaning in a regular expression; if you want to match a literal |, you just need to escape it:

$foo =~ s/ \| lastname/fillertext/;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜