[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: how to determine if a string is printable?

Bill Kelly

5/16/2007 1:42:00 AM

From: "Mike Steiner" <mikejaysteiner@gmail.com>
>
> Is there an easy way to test if a string is printable (doesn't contain
> control characters)? Something easier than listing out all the letters,
> numbers, and punctuation and checking each character.

Based on: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...

To test for non-printable characters, use:

str =~ /[^[:print:]]/



Regards,

Bill



4 Answers

Michael W. Ryder

5/16/2007 7:34:00 AM

0

Bill Kelly wrote:
> From: "Mike Steiner" <mikejaysteiner@gmail.com>
>>
>> Is there an easy way to test if a string is printable (doesn't contain
>> control characters)? Something easier than listing out all the letters,
>> numbers, and punctuation and checking each character.
>
> Based on:
> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>
> To test for non-printable characters, use:
>
> str =~ /[^[:print:]]/
>
>
Even though I am not the OP I tried that and using index but none of
them seem to work. I get an error message saying "warning character
class has '[' without escape" followed by "warning regexp has ']'
without escape" and the return is nil regardless what is in the string.
Is there something I am missing here?


>
> Regards,
>
> Bill
>
>
>

Bill Kelly

5/16/2007 7:54:00 AM

0

From: "Michael W. Ryder" <_mwryder@worldnet.att.net>
> Bill Kelly wrote:
>>
>> Based on:
>> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>>
>> To test for non-printable characters, use:
>>
>> str =~ /[^[:print:]]/
>>
> Even though I am not the OP I tried that and using index but none of
> them seem to work. I get an error message saying "warning character
> class has '[' without escape" followed by "warning regexp has ']'
> without escape" and the return is nil regardless what is in the string.
> Is there something I am missing here?

Not sure. It works for me, without warnings even with ruby -w on:
ruby 1.8.4 (2005-12-24) [i386-mswin32]

And in IRB:

irb(main):117:0> "abc\ndef" =~ /[^[:print:]]/
=> 3 # yes, a non-printable character was found at idx 3

irb(main):118:0> "abcdef" =~ /[^[:print:]]/
=> nil # no, a non-printable character was not found


Regards,

Bill



Michael W. Ryder

5/16/2007 8:00:00 AM

0

Bill Kelly wrote:
> From: "Michael W. Ryder" <_mwryder@worldnet.att.net>
>> Bill Kelly wrote:
>>>
>>> Based on:
>>> http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
>>>
>>> To test for non-printable characters, use:
>>>
>>> str =~ /[^[:print:]]/
>>>
>> Even though I am not the OP I tried that and using index but none of
>> them seem to work. I get an error message saying "warning character
>> class has '[' without escape" followed by "warning regexp has ']'
>> without escape" and the return is nil regardless what is in the string.
>> Is there something I am missing here?
>
> Not sure. It works for me, without warnings even with ruby -w on:
> ruby 1.8.4 (2005-12-24) [i386-mswin32]
>
> And in IRB:
>
> irb(main):117:0> "abc\ndef" =~ /[^[:print:]]/
> => 3 # yes, a non-printable character was found at idx 3
>
> irb(main):118:0> "abcdef" =~ /[^[:print:]]/
> => nil # no, a non-printable character was not found
>
>

OK, I see what I did. I forgot the trailing colon. Still getting used
to the language and regular expressions.

> Regards,
>
> Bill
>
>
>

Rick DeNatale

5/17/2007 3:39:00 PM

0

On 5/16/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote:

>
> OK, I see what I did. I forgot the trailing colon. Still getting used
> to the language and regular expressions.

Regular expression syntax is probably best thought of as a language of
it's own. Or perhaps a family of languages since there are so many
dialects.

http://www.regular-express...

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...