Re: substituted text is being interpreted
by gopalr (Priest) on Jan 06, 2005 at 15:34 UTC
|
Hi,
what dragonchild said is correct... What variable are you substituting on?
if Metacharacter found inside the variable use 'quotemeta' or '\Q' and '\E'
$line='Sampel Text <a href="somefilename(x)withanumberedhash.htm#666">
+';
$somethingelse='ok';
$var = '<a href="somefilename(x)withanumberedhash.htm#666">';
$var=quotemeta($var);
$line=~s@$var@$somethingelse@;
or
$line=~s@\Q$var\E@$somethingelse@;
| [reply] [d/l] [select] |
Re: substituted text is being interpreted
by ccn (Vicar) on Jan 06, 2005 at 14:57 UTC
|
use \Q, \E modifires in the regexp. see perldoc perlre
| [reply] |
Re: substituted text is being interpreted
by holli (Abbot) on Jan 06, 2005 at 15:03 UTC
|
mmh.
this runs as expected:
$somethingelse="AA";
$var = 'BB';
$_ = 'beforeBBafter';
s@$var@$somethingelse@;
print; #prints "beforeAAafter"
but this does not:
$somethingelse="AA";
$var = '<a href="somefilename(x)withanumberedhash.htm#666">';
$_ = 'before<a href="somefilename(x)withanumberedhash.htm#666">after';
s@$var@$somethingelse@;
print;
and that is because of the braces() which are regex special chars.
if you
$somethingelse="AA";
$var = '<a href="somefilename\(x\)withanumberedhash.htm#666">';
$_ = 'before<a href="somefilename(x)withanumberedhash.htm#666">after';
s@$var@$somethingelse@;
print;
it works fine. | [reply] [d/l] [select] |
|
Interesting. I thought using single quotes meant you didn't have to escape special characters. Is this because the regex grabs the single quoted text and then interprets it as a normal (double-quoted) expression?
--
Linux, sci-fi, and Nat Torkington, all at Penguicon 3.0
perl -e 'print(map(chr,(0x4a,0x41,0x50,0x48,0xa)))'
| [reply] |
|
It's a bit subtler than that.
When you write string literals, single-quoting them make them contain exactly the characters you typed, while double-quoting them activates the interpolation mechanism.
On the other hand, when you use a string as a regular expression, it gets interpreted by the regex engine.
For example:
$literal='abc@array$';
@array=(1,2,3);
$interpolated="abc@array\$";
print 'literal: ',$literal,"\n";
print 'interp.: ',$interpolated,"\n";
if ($literal =~ /$literal/) {
print "literal matches itself\n"
} else {
print "literal doesn't match itself\n"
}
$literal2='abc@array';
if ($literal2 =~ /$literal2/) {
print "literal2 matches itself\n"
} else {
print "literal2 doesn't match itself\n"
}
if ($literal2 =~ /$literal/) {
print "literal2 matches literal\n"
} else {
print "literal2 doesn't match literal\n"
}
This prints:
literal: abc@array$
interp.: abc1 2 3$
literal doesn't match itself
literal2 matches itself
literal2 matches literal
The first two lines are hopefully obvious. As for the others:
- $literal does not match itself because, seen as a regexp, it requires the string it matches to end with a y (the $ is interpreted by the regexp engine)
- $literal2 does match itself. This is to confirm that the regexp is not "double-quote-interpolated-expanded-whathaveyou": the @ is still a single character.
- $literal2 matches $literal because it does end in a y.
OK, for the pedants: the string between the // is interpolated, but its contents are not further expanded. I felt that this was more noise than signal, in this case.
| [reply] [d/l] [select] |
Re: substituted text is being interpreted
by dave_the_m (Monsignor) on Jan 06, 2005 at 15:05 UTC
|
s@$var@$somethingelse@
I presume you want the replacement text to be the
literal string '$somethingelse', rather
than the contents of the variable. The replacement text in
substitutions behave like
double-quoted strings, which cause interpolation. To avoid
this, either use single quotes, or escape the $, eg
s{$var}'$somethingelse';
s@$var@\$somethingelse@;
Dave. | [reply] [d/l] |
Re: substituted text is being interpreted
by duff (Parson) on Jan 06, 2005 at 14:56 UTC
|
I'm sorry, but you're example doesn't elucidate your problem (at least for me). What do you mean by "the text has been interpreted"? What does your example do that you expect it to not do or vice versa?
update:Ah, I think ccn has the sense of it. Some of the characters in your string are special WRT the regular expression engine (and presumably you want them to be treated as-is). You can use quotemeta or \Q as ccn says.
| [reply] [d/l] |
Re: substituted text is being interpreted
by dragonchild (Archbishop) on Jan 06, 2005 at 14:56 UTC
|
What variable are you substituting on? Regular expressions don't operate in a void, you know.
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] |
|
yes, they do. if no variable is given, then they operate on $_.
| [reply] [d/l] |
|
Very true, but where is $_ referenced in the OP?
Being right, does not endow the right to be rude; politeness costs nothing. Being unknowing, is not the same as being stupid. Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence. Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.
| [reply] |
|
Re: substituted text is being interpreted
by j.goor (Acolyte) on Jan 07, 2005 at 13:05 UTC
|
Hi All,
quotemeta did the trick!!
Thanx for meditating! I feel actually enlightened! Wheeeee!!! ... ehhh.. I mean "Hummmmmmmmmmmmm" ;-)
Rgdz,
John | [reply] |