Perl hash substitution with special characters in keys

爱⌒轻易说出口 提交于 2019-12-02 13:58:54

Problem 1

You use the pattern a* thinking it will match only a*, but a* means "0 or more a". You can use quotemeta to convert text into a regex pattern that matches that text.

Replace

my $keys = join '|', keys %stimhash;

with

my $keys = join '|', map quotemeta, keys %stimhash;

Problem 2

\b

is basically

(?<!\w)(?=\w)|(?<=\w)(?!\w)

But * (like the space) isn't a word character. The solution might be to replace

s/($keys)\b/$stimhash{$1}/g

with

s/($keys)(?![\w*])/$stimhash{$1}/g

though the following make more sense to me

s/(?<![\w*])($keys)(?![\w*])/$stimhash{$1}/g

Personally, I'd use

s{([\w*]+)}{ $stimhash{$1} // $1 }eg
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!