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

前端 未结 5 1254
轮回少年
轮回少年 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:05

    There is a function for that quotemeta.

    quotemeta EXPR
    Returns the value of EXPR with all non-"word" characters backslashed. (That is, all characters not matching /[A-Za-z_0-9]/ will be preceded by a backslash in the returned string, regardless of any locale settings.) This is the internal function implementing the \Q escape in double-quoted strings.

    If EXPR is omitted, uses $_.

提交回复
热议问题