Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am having a problem trying to remove the ('single quote) from the text here, my regular expression is ignoring it, any reason for that?
Here is the line I am using to remove or to try to remove the single quote from the text.
$item->{'description'}=~s/\n//gms; $item->{'description'}=~s/'//gms; # Like in here - Benefit's - + I am trying to get rid of the single quote but it is not working.

Here is some data
<font size=2><b><a target="_blank" href="http://us.rd.yahoo.com/dailyn +ews/rss/topstories/*http://story.news.yahoo.com/news?tmpl=story2&u=/l +atimests/20050210/ts_latimes/drugbenefitscostestimatessoarsurprise">D +rug Benefit's Cost Estimates Soar, Surprise (Los Angeles Times)</a></ +b></font><br /><font size=1></font><br />

Replies are listed 'Best First'.
Re: Removing Single Quote Problem
by Roy Johnson (Monsignor) on Feb 10, 2005 at 18:57 UTC
    A better tool for removing characters when you don't need regular expressions is tr///.
    $item->{'description'} =~ tr/\n'//d;
    But your s/// expression should be removing it. Are you sure it's not a "smart quote"?

    Caution: Contents may have been coded under pressure.
      Watch out for removing newlines willy-nilly. In HTML spans, a newline and a space are equivalent.

      Removingthe newlineswhere thereis noother whitespacewill givesome poorresults.

      --
      [ e d @ h a l l e y . c c ]

Re: Removing Single Quote Problem
by hubb0r (Pilgrim) on Feb 10, 2005 at 18:55 UTC
    When I run your code as follows:

    #!/usr/local/bin/perl -w use strict; my $item = {}; $item->{'description'} = qq{<font size=2><b><a target="_blank" href=" +http://us.rd.yahoo.com/dailynews/rss/topstories/*http://story.news.ya +hoo.com/news?tmpl=story2&u=/latimests/20050210/ts_latimes/drugbenefit +scostestimatessoarsurprise">Drug Benefit's Cost Estimates Soar, Surpr +ise (Los Angeles Times)</a></b></font><br /><font size=1></font><br / +>}; $item->{'description'} =~ s/'//gms; print $item->{'description'}; ---OUTPUT--- <font size=2><b><a target="_blank" href="http://us.rd.yahoo.com/dailyn +ews/rss/topstories/*http://story.news.yahoo.com/news?tmpl=story2&u=/l +atimests/20050210/ts_latimes/drugbenefitscostestimatessoarsurprise">D +rug Benefits Cost Estimates Soar, Surprise (Los Angeles Times)</a></b +></font><br /><font size=1></font><br />

    In what instance is it not working? Ps - even though there was only one single quote in your data, this code will also work without a problem with multiple single quotes due to the /g;
Re: Removing Single Quote Problem
by phaylon (Curate) on Feb 10, 2005 at 19:22 UTC
    Another one without re's (Hm, I must be in mood for this..):
    use warnings; use strict; my $text = "I think there's a quote in here..\n"; + my $p; substr( $text, $p - 1, 1 ) = '' while $p = index( $text, "'" ) + 1; + print $text;
      Still having problems, here is more of the code
      foreach my $item (@{$rss->{'items'}}) { next unless defined($item->{'title'}) && defined($item->{'link +'}) && defined($item->{'description'}); $i += 1; next if $i > $feedcount; $item->{'description'}=~s/\n//gms; #$item->{'description'} =~ tr/\n'//d; $item->{'description'}=~s/'//gms; $output .= "'<font size=2><b><a target=\"_blank\" href=\"$item +->{'link'}\">$item->{'title'}</a></b></font><br /><font size=1>$item- +>{'description'}</font><br />'+\n"; }
        What problems? I would also recommend reading perlstyle
        next unless defined($item->{'title'}) && defined($item->{'link +'}) && defined($item->{'description'});
        could be written as
        next unless defined $item->{title} and defined $item->{link} and defined $item->{description};
        Also, why are you setting $output after trying to strip the singlequotes?

        Ordinary morality is for ordinary people. -- A.C.
Re: Removing Single Quote Problem
by jpk236 (Monk) on Feb 10, 2005 at 19:00 UTC
    seems to work okay for me
    #!/usr/bin/perl -w use strict; my $text = "hi -- this has a ' in it and I want ' to go away"; $text =~ s/'//gms; print "$text\n";
    Output:
    hi -- this has a in it and I want to go away
    - Justin
Re: Removing Single Quote Problem
by Anonymous Monk on Feb 10, 2005 at 21:38 UTC
    $item->{'description'}=~s/'//gms;
    The /m and /s modifiers don't serve any purpose in this regex. To include them smacks of cargo cultism.