How can I convert a string into a regular expression that matches itself in Perl?

前端 未结 5 1211
轮回少年
轮回少年 2021-01-13 20:43

How can I convert a string to a regular expression that matches itself in Perl?

I have a set of strings like these:

Enter your selection:
Enter Code          


        
5条回答
  •  别那么骄傲
    2021-01-13 20:59

    As Brad Gilbert commented use quotemeta:

    my $regex = qr/^\Q$string\E$/;
    

    or

    my $quoted = quotemeta $string;
    my $regex2 = qr/^$quoted$/;
    

提交回复
热议问题