substitution using variables

Discussion in 'Perl Misc' started by GarfGarf, Nov 16, 2005.

  1. GarfGarf

    GarfGarf Guest

    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.
    GarfGarf, Nov 16, 2005
    #1
    1. Advertising

  2. GarfGarf

    Eden Cardim Guest

    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/
    Eden Cardim, Nov 16, 2005
    #2
    1. Advertising

  3. GarfGarf

    Guest

    my $four="\\(who is black";
    , Nov 16, 2005
    #3
  4. 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
    it_says_BALLS_on_your forehead, Nov 16, 2005
    #4
  5. 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
    Tad McClellan, Nov 16, 2005
    #5
  6. 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
    John W. Krahn, Nov 16, 2005
    #6
  7. GarfGarf

    GarfGarf Guest

    Thanks to all that replied.
    The \Q \E was exactly what i needed.
    GarfGarf, Nov 17, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian
    Replies:
    4
    Views:
    2,297
    Ben Bacarisse
    Feb 2, 2006
  2. Scott
    Replies:
    1
    Views:
    857
    Martin Honnen
    Jul 8, 2004
  3. Replies:
    9
    Views:
    929
  4. Dietrich
    Replies:
    3
    Views:
    143
    Joe Smith
    Jul 25, 2004
  5. Wim

    variables in substitution

    Wim, Jan 15, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    93
    Brian McCauley
    Jan 16, 2007
Loading...

Share This Page