in reply to Possible Match Problem
Basically if you want to match upon string where one or more characters would mean something to a Perl regex, you need to say that: "I don't want these special characters to count, i.e. mean something in a regex sense" - otherwise when Perl interpolates that string, those characters "will count". There are a couple of ways to say "please use all these characters verbatim", I showed one of these.#!/usr/bin/perl -w use strict; my $str1 = "Hi("; my $str2 = "hi"; if ($str2 =~ /\Q$str1\E/) #\Q...\E means like qr #don't interpret characters #like '(' within $str1 #use them verbatim { print "Match: Yes\n"; } else { print "Match: No\n"; } #prints Match: No $str2 = "hI("; if ($str2 =~ /\Q$str1\E/i) #/i means case insensitive { print "Match: Yes\n"; } else { print "Match: No\n"; } #prints Match: Yes
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Possible Match Problem
by AnomalousMonk (Archbishop) on Jan 19, 2012 at 06:05 UTC | |
by Marshall (Canon) on Jan 19, 2012 at 08:30 UTC | |
by AnomalousMonk (Archbishop) on Jan 19, 2012 at 16:07 UTC |