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

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

    Why use a regular expression at all? Since you aren't doing any capturing and it seems you will not be going to allow for any variations, why not simply use the index builtin?

    $s1 = 'hello, (world)?!';
    $s2 = 'he said "hello, (world)?!" and nothing else.';
    
    if ( -1 != index  $s2, $s1 ) {
        print "we've got a match\n";
    }
    else {
        print "sorry, no match.\n";
    }
    

提交回复
热议问题