Dave | 23 Jul 01:00

Re: How to detect if a string contains any funny characters from non English alphabets


Well I looked at the API and thought that  str.each_char {} might
work, but it's not recognized in my version of Ruby (1.8.6 on Ubuntu)
so that seems to be a dead end. Here's an ugly hack that works though:

def nonroman_test(str)
  if nonroman(str) then
    puts "#{str} has nonroman characters!"
  else
    puts "#{str} does not have nonroman characters!"
  end
end

def nonroman (str)
  (/^[\w\s!@#\$%\^\\&*()\]\[,.?]*$/ =~ str) == nil
end

nonroman_test("abc")
nonroman_test("abcá´š")

nonroman(str) return true if the string contains any characters
besides letters, digits, whitespace, and the following: !@#$%^&*()
[],.?

You can alter the regular expression to change what is allows. Just
add any additional allowed characters before the final ] on the line
in nonroman(). Some characters may need to have a \ in front of them
to work.

Hope that helps!
(Continue reading)


Gmane