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

前端 未结 5 1205
轮回少年
轮回少年 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 21:03

    From http://www.regular-expressions.info/characters.html :

    there are 11 characters with special meanings: the opening square bracket [, the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening round bracket ( and the closing round bracket )

    In Perl (and PHP) there is a special function quotemeta that will escape all these for you.

提交回复
热议问题