in reply to Regex to delete high-bit characters

s/[^[:ascii:]]+//g;

Replies are listed 'Best First'.
Re^2: Regex to delete high-bit characters
by graff (Chancellor) on Oct 05, 2007 at 21:39 UTC
    That would be a good solution for any reasonably current version of Perl. I'm not exactly sure when the POSIX [:blah:] notations were incorporated into perl's regex engine, but I know some people are running perl environments where it is not available:
    $ perl -v This is perl, version 5.005_03 built for sun4-solaris Copyright 1987-1999, Larry Wall ... $ perl -le '$_="abc"; print "ok" if (/^[[:ascii:]]+$/)' # (no output -- that char.class doesn't work) $ perl -le '$_="abc"; print "ok" if (/^[\x00-\x7f]+$/)' ok

    The OP didn't actually say how far back he needs to go with his perl version(s), but I'm not surprised if 5.005 is part of his condition.

    (Update: forgot to mention: that same sun4-solaris machine also has perl 5.8.0 installed as a non-default version, and the first one-liner works with that as expected:

    $ perl5.8.0 -le '$_="abc"; print "ok" if (/^[[:ascii:]]+$/)' ok