in reply to How do I determine if a string contains non ascii characters

That's pretty much the best way to do it. You done good.

japhy -- Perl and Regex Hacker
  • Comment on Re: How do I determine if a string contains non ascii characters

Replies are listed 'Best First'.
Re: Re: How do I determine if a string contains non ascii characters
by Anonymous Monk on Jul 13, 2001 at 16:00 UTC
    Thanks. I couldn't think of any other sensible way of doing it but I wanted to make sure I wasn't missing anything obvious.
    It would have been nice if there was already a pre-defined character class for this though.(since \A and \a are already taken so I suppose \I and \i?)
      There is a POSIX macro for this: [:ascii:]. That is NOT a character class in and of itself -- it must be used IN a character class:
      if ($string =~ /[^[:ascii:]]/) { # a non-ASCII character was found! }


      japhy -- Perl and Regex Hacker

        Or use the negated POSIX (perlish addition) 'character class' directly: if ($string =~ /[[:^ascii:]]//) {

        -- Hofmator

(tye)Re: How do I determine if a string contains non ascii characters
by tye (Sage) on Jul 13, 2001 at 19:18 UTC

    Agreed. Though things that have problems with "8-bit characters" often also have problems with nul bytes and some control characters so you might consider /[^ -~\s]/ which leaves out nul, "\x7f", and all of the control characters except for the usual "\t\f\r\n". Just depends on what you need. (:

            - tye (but my friends call me "Tye")