开发者

How to check for | in doing substitution in Perl

开发者 https://www.devze.com 2023-03-02 10:15 出处:网络
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

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

精彩评论

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