in reply to Regular Expressions with varialbe with "[]"
You need to escape the ['s and ]'s. qr"..." and \Q and \E are useful for that. Consider:
which results in:my $haystack = 'lets test[1] this thing $string'; my @tests = ('test[1]', '\Etest[1]\Q', 'test\[1\]'); @tests = map {$_, qr"\Q$_\E"} @tests; print "Check $_:" . ($haystack =~ /($_)/ ? "got $1\n" : "no match\n") +for @tests;
Check test[1]:no match Check (?^:test\[1\]):got test[1] Check \Etest[1]\Q:no match Check (?^:\\Etest\[1\]\\Q):no match Check test\[1\]:got test[1] Check (?^:test\\\[1\\\]):no match
|
|---|