substitution using variables

G

GarfGarf

Hi all,

I'm trying to remove from a string another string that is contained in
a variable. The contents of the variable could be anything. However, if
the varibale contains certain characters the substibution fails..

e.g.

#!/usr/bin/perl -w

my $main="the cat sat on the mat (who is black)";
my $one="the";
my $two="cat";
my $three="(who is black)";
my $four="(who is black"; #notice deliberate missing last bracket

try($main,$one);
try($main,$two);
try($main,$three);
try($main,$four);


sub try {
my ($in,$pat)=@_;

$in=~s/$pat//;
print "$in\n";
}



when run it gives:
cat sat on the mat (who is black)
the sat on the mat (who is black)
the cat sat on the mat ()
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE who is black/
at /tmp/try.pl line 18.


how can I prevent this.. and allow item four to work?

Thanks.
 
E

Eden Cardim

GarfGarf escreveu:

--snip--
my $four="(who is black"; #notice deliberate missing last bracket
--snip--
sub try {
my ($in,$pat)=@_;

$in=~s/$pat//;
print "$in\n";
} --snip--
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE who is black/
at /tmp/try.pl line 18.


how can I prevent this.. and allow item four to work?

This is inevitable, as far as I know, the regexp engine interpolates
/$pat/ into the regexp /(who is black/ which is invalid, since
unescaped parentheses are grouping constructs and must be balanced for
the regexp to be valid. I suppose you're trying to match this: /\(who
is black/
 
I

it_says_BALLS_on_your forehead

GarfGarf said:
Hi all,

I'm trying to remove from a string another string that is contained in
a variable. The contents of the variable could be anything. However, if
the varibale contains certain characters the substibution fails..

e.g.

#!/usr/bin/perl -w

my $main="the cat sat on the mat (who is black)";
my $one="the";
my $two="cat";
my $three="(who is black)";
my $four="(who is black"; #notice deliberate missing last bracket

try($main,$one);
try($main,$two);
try($main,$three);
try($main,$four);


sub try {
my ($in,$pat)=@_;

$in=~s/$pat//;

when the regex engine interpolates $pat, it literally replaces the
variable with the contents of the variable, so:

$in=~s/$pat//;

becomes (when $pat eq $four)

$in=~s/(who is black//;

and the regex engine needs a matching paren to know when to stop
capturing.

you need to escape the special characters using \Q (start escaping) and
\E (end escaping)

$in=~s/\Q$pat\E//;
print "$in\n";
}



when run it gives:
cat sat on the mat (who is black)
the sat on the mat (who is black)
the cat sat on the mat ()
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE who is black/
at /tmp/try.pl line 18.


how can I prevent this.. and allow item four to work?

HTH
 
T

Tad McClellan

GarfGarf said:
I'm trying to remove from a string another string that is contained in
a variable.


You don't need regular expressions for that.

$in=~s/$pat//;


$in =~ s/\Q$pat//;


how can I prevent this..


Use substr() instead of m//

----------------
sub try {
my ($in,$pat)=@_;

substr $in, index($in,$pat), length $pat, '';
print "$in\n";
}
 
J

John W. Krahn

GarfGarf said:
I'm trying to remove from a string another string that is contained in
a variable. The contents of the variable could be anything. However, if
the varibale contains certain characters the substibution fails..

e.g.

#!/usr/bin/perl -w

my $main="the cat sat on the mat (who is black)";
my $one="the";
my $two="cat";
my $three="(who is black)";
my $four="(who is black"; #notice deliberate missing last bracket

try($main,$one);
try($main,$two);
try($main,$three);
try($main,$four);


sub try {
my ($in,$pat)=@_;

$in=~s/$pat//;
print "$in\n";
}


when run it gives:
cat sat on the mat (who is black)
the sat on the mat (who is black)
the cat sat on the mat ()
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE who is black/
at /tmp/try.pl line 18.


how can I prevent this.. and allow item four to work?

Like this:

sub try {
my ( $in, $pat ) = @_;

return if ( my $i = index $in, $pat ) < 0;
substr $in, $i, length( $pat ), '';
print "$in\n";
}



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top