如何在插入Perl正则表达式的字符串中转义特殊字符? [英] How do I escape special chars in a string I interpolate into a Perl regex?
本文介绍了如何在插入Perl正则表达式的字符串中转义特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,其中可能包含特殊字符,例如:$
,(
,@
,#,
等.
我需要能够对该字符串执行正则表达式.
I have a string which may hold special characters like: $
, (
, @
, #,
etc.
I need to be able to perform regular expressions on that string.
现在,如果我的字符串中包含这些字符中的任何一个,则正则表达式似乎会中断,因为这些是正则表达式的保留字符.
Right now if my string has any of these characters the regex seems to break since these are reserved characters for regex.
有人知道一个很好的子例程,可以很好地对我的任何这些字符进行转义,以便以后我可以执行以下操作:
Does anybody knows a good subroutine that would escape nicely any of these characters for me so that later I could do something like:
$p_id =~ /^$key/
推荐答案
$p_id =~ /^\Q$key\E/;
这篇关于如何在插入Perl正则表达式的字符串中转义特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文