Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > substitution using variables

Reply
Thread Tools

substitution using variables

 
 
GarfGarf
Guest
Posts: n/a
 
      11-16-2005
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.

 
Reply With Quote
 
 
 
 
Eden Cardim
Guest
Posts: n/a
 
      11-16-2005

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/

 
Reply With Quote
 
 
 
 
xicheng@gmail.com
Guest
Posts: n/a
 
      11-16-2005
my $four="\\(who is black";

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      11-16-2005

GarfGarf wrote:
> 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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-16-2005
GarfGarf <> wrote:

> 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";
}
----------------


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      11-16-2005
GarfGarf wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
GarfGarf
Guest
Posts: n/a
 
      11-17-2005
Thanks to all that replied.
The \Q \E was exactly what i needed.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
variables in substitution Wim Perl Misc 4 01-16-2007 08:58 AM
Question on regex substitution using variables... Ian Perl 4 02-02-2006 10:49 PM
Over Interpolation of Variables in a Substitution Dietrich Perl Misc 3 07-22-2004 11:22 PM
Help with variable substitution using XSL Scott XML 1 07-08-2004 06:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57