Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Removing \x092 with a regex

by wfsp (Abbot)
on Jul 26, 2005 at 14:55 UTC ( [id://478230]=perlquestion: print w/replies, xml ) Need Help??

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

How can I remove/replace a ’ (\x092)?
my $str = q|cat’s|; $str =~ s/\x092/'/; print "$str\n"; # outputs: # cat’s
The ’ could be anywhere in the string.

winxp/activestate 5.8.7.

Thanks in advance

Update:
\x{092} produces the same result

Udate 2

$str =~ tr/\x{092}/'/d;
The same (with or without the braces)

Replies are listed 'Best First'.
Re: Removing \x092 with a regex
by kwaping (Priest) on Jul 26, 2005 at 15:10 UTC
    This might be silly, but why can't you just do this?
    $str =~ s/’/'/;
    That works for me.
      Not silly at all and it works for me too.
      It's just that I can't see the damn thing in my editor :-(
Re: Removing \x092 with a regex
by davido (Cardinal) on Jul 26, 2005 at 15:47 UTC

    If you're trying to remove all \x{92} characters with a regexp, be sure to use the /g modifier. However, if that's your intent, use the transliteration operator instead; that's what it's designed for, and it's more efficient at its job:

    $str =~ tr/\x{092}/'/d;

    That ought to do the trick.


    Dave

Re: Removing \x092 with a regex
by polettix (Vicar) on Jul 26, 2005 at 16:52 UTC
    Linux, perl v. 5.8.6 self-compiled - for what it can help.

    In the first case you would be biten by the fact that the \xNN form works only when you have two hex chars, not three as in 092. So, the fact that you always get the same output is the real puzzle here.

    #!/usr/bin/perl use strict; use warnings; my $str = q|cat’s|; printit($str, 'original'); (my $str1 = $str) =~ s/\x092/'/; printit($str1, 'hex only two nybbles'); (my $str2 = $str) =~ s/\x92/'/; printit($str2, 'seems good here'); (my $str3 = $str) =~ s/\x{092}/'/; printit($str3, 'seems good here'); (my $str4 = $str) =~ tr/\x{092}/'/d; printit($str4, 'seems good here'); sub printit { my ($v, $msg) = @_; my $h = unpack "H*", $v; $h =~ s/(..)/0x$1 /g; print "$h\t$v\t$msg\n"; } __END__ 0x63 0x61 0x74 0x92 0x73 cat?s original 0x63 0x61 0x74 0x92 0x73 cat?s hex only two nybbles 0x63 0x61 0x74 0x27 0x73 cat's seems good here 0x63 0x61 0x74 0x27 0x73 cat's seems good here 0x63 0x61 0x74 0x27 0x73 cat's seems good here
    Now, it would be good to see how this script runs on your machine, in particular to see the hex dumps. The comment messages will become wrong of course :)

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
      Many thanks for your efforts

      I added a test suggested by kwaping above.

      #!/usr/bin/perl use strict; use warnings; my $str = q|cat’s|; printit($str, 'original'); (my $str1 = $str) =~ s/\x092/'/; printit($str1, 'hex only two nybbles'); (my $str2 = $str) =~ s/\x92/'/; printit($str2, 'seems good here'); (my $str3 = $str) =~ s/\x{092}/'/; printit($str3, 'seems good here'); (my $str4 = $str) =~ tr/\x{092}/'/d; printit($str4, 'seems good here'); (my $str5 = $str) =~ s/’/'/; printit($str5, 'added'); sub printit { my ($v, $msg) = @_; my $h = unpack "H*", $v; $h =~ s/(..)/0x$1 /g; print "$h\t$v\t$msg\n"; } __END__ 0x63 0x61 0x74 0xe2 0x80 0x99 0x73 cat’s original 0x63 0x61 0x74 0xe2 0x80 0x99 0x73 cat’s hex only two nybbles 0x63 0x61 0x74 0xe2 0x80 0x99 0x73 cat’s seems good here 0x63 0x61 0x74 0xe2 0x80 0x99 0x73 cat’s seems good here 0x63 0x61 0x74 0xe2 0x80 0x99 0x73 cat’s seems good here 0x63 0x61 0x74 0x27 0x73 cat's added

        You're saving your Perl script as a UTF-8 file but not telling Perl that it is supposed to be reading the script as such. (I bet)

        - tye        

Re: Removing \x092 with a regex
by Anonymous Monk on Jul 26, 2005 at 14:57 UTC
    Use \x{092}.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://478230]
Approved by Tanalis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found