perl string escape [closed]

╄→гoц情女王★ 提交于 2019-12-10 12:25:51

问题


I wish to escape special characters susch as quotes and spaces in a Perl string.

I'd like to avoid using regex and installing extra modules.


回答1:


Using quotemeta may help you.

my $escaped = quotemeta $string_with_quotes_and_spaces;

which will escape with a backslash anything that isn't alphanumeric or an underscore.




回答2:


If you are receiving the string then Borodin's solution with quotemeta will work.

If you are declaring the string, you can use the qq switch to declare your own string delimiter. For example to use # instead of ' or ":

my $string = qq#Didn't know I could do this!"how" amazing#;

This will escape the usual string delimiters.



来源:https://stackoverflow.com/questions/11666858/perl-string-escape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!