I want to switch off the significance of special characters when matching an expression in perl, i.e. I have a line that says:-
if (@_[0] =~ /$a/i)
(isn't perl horrible!) and I want a '.' in the $a to be actually matched. I can't easily escape the '.' with a '' because the $a is also used as a filename and if I put the '' I get a filename with a '' in it as well which I don't want.
All I actually want is a case insensitive string comparison with no characters having any special meaning at all.
On Mon, Mar 26, 2007 at 12:00:51PM +0100, Brett Parker wrote:
On Mon, Mar 26, 2007 at 11:40:21AM +0100, List Mail wrote:
I want to switch off the significance of special characters when matching an expression in perl, i.e. I have a line that says:-
if (@_[0] =~ /$a/i)
Try: if (@_[0] =~ /\Q$a\E/i)
Yes, thanks to you and Martijn, that's exactly what I want.
It is a sub-string match so I can't just test for equality (I hadn't realised that until I dug into the code a little).
List Mail (!) wrote:
I want to switch off the significance of special characters when matching an expression in perl,
man perlre, search for quotemeta.
my $a = 'we$rd*f?le.txt'; if ('we$rd*f?le.TXT' =~ /\Q$a\E/i) { print "yup\n"; } if ('we$rd*f?le_txt' =~ /\Q$a\E/i) { print "no, silly\n"; }
Note that you're still doing a substring match, which may or may not be what you want.
All I actually want is a case insensitive string comparison with no characters having any special meaning at all.
Then why not just downcase and compare:
if (lc('we$rd*f?le.TXT') eq lc($a)) { print "of course\n"; }
-- Martijn