Re: matching the + character
by dragonchild (Archbishop) on Aug 06, 2001 at 21:45 UTC
|
Because you're using double-quotes (""), you need to backslash the backslash. :) If you used single-quotes ('') like 'a\+b', you'll be just fine.
The lesson here is to use the minimal quoting mechanism. Unless you're interpolating variables, use ''. If you're interpolating variables, remember that everything is interpolated.
------ /me wants to be the brightest bulb in the chandelier! | [reply] |
Re: matching the + character
by thpfft (Chaplain) on Aug 06, 2001 at 22:35 UTC
|
Single quoting and character classes are all very well if you're dealing with test data, but i think we can assume you're not really going to be testing whether two strings you just typed in match. In the general case, if you want to find out whether $var2 is contained in $var1, and $var2 might contain something that will be unhelpfully interpolated, use this:
$match if $var1 =~ /\Q$var2\E/;
\Q..\E is a special case of the quotemeta function, which automatically escapes non-alphanumerics (but not $ or @, so you still get variable interpolation).
Not the first time this has come up, by the way. Reading perlre will help.
update: followed own sanctimonious advice, read perlre, changed description of quotemeta :(
| [reply] [d/l] |
Re: matching the + character
by suaveant (Parson) on Aug 06, 2001 at 21:43 UTC
|
+ is a special character in regular expressions that means match one or more of what is before me... your regexp will match one or more a's followed by a b... i.e. aaab ab aaaaaab, to match a plus you must escape it, using a \ so \+ will match a plus... see perlre
Update Doh, my eyes skipped the lower text... :)
you need the \\ because "" interploates data, thus converting \+ to + before it gets to the regexp... if you use single quotes 'a\+b' then you will be ok.
- Ant
- Some of my best work - Fish Dinner
| [reply] |
|
use strict;
3 my $var1 = "a+b";
4 my $var2 = "a+b";
5 if ($var1 =~ /\Q$var2\E/) {
6 print "hey it matches \n";
7 }
8
9 print "hey here \n";
10
See Line : 5 | [reply] [d/l] |
|
I know that thats i even tried matching by setting
use strict;
my $var1 = "a+b";
my $var2 = "a\+b";
if ($var1 =~ /$var2/) {
print "hey it matches \n";
}
print "hey here \n";
but it doesn't work. AGAIN the only thing that works is
$var2 = "a\\+b";
please explain
Waris | [reply] [d/l] [select] |
Re: matching the + character
by kilinrax (Deacon) on Aug 06, 2001 at 21:47 UTC
|
In a double quoted string, '\+' will be interpreted as a character escape. Since '\+' doesn't actually have any special character escape meaning, it is interpreted as simply '+'. If you want to pass 'a\+b' to the regex, you'll have to use a second slash to escape the first and prevent any attempt at character escape interpretation gobbling up the '\'. | [reply] |
Re: matching the + character
by lshatzer (Friar) on Aug 06, 2001 at 22:02 UTC
|
Along with what everyone else has said above, the function quotemeta comes to mind. Check it out, it will help you when you have other characters you need to quote properly also... | [reply] |
Re: matching the + character
by dga (Hermit) on Aug 06, 2001 at 22:12 UTC
|
use strict;
my $var1 = "a+b";
my $var2 = "a[+]b";
if ($var1 =~ /$var2/) {
print "hey it matches \n";
}
print "hey here \n";
| [reply] [d/l] |
Re: matching the + character
by foogod (Friar) on Aug 06, 2001 at 22:16 UTC
|
use strict;
my $var1 = 'a+b';
my $var2 = 'a+b';
if ($var1 =~ /$var2/) {
print "hey it matches \n";
}
print "hey here \n";
the posts above already said why it isnt working ... but now you have code that will actaully work (that is like yours).
- f o o g o d
--- ruining the bell curve for everyone else --- | [reply] [d/l] |
Re: matching the + character
by mamboking (Monk) on Aug 07, 2001 at 01:50 UTC
|
Do you really need to use a regexp? Perhaps you could just use index() or even "eq". | [reply] [d/l] [select] |