23 Jul 01:00
Re: How to detect if a string contains any funny characters from non English alphabets
From: Dave <davidbalves@...>
Subject: Re: How to detect if a string contains any funny characters from non English alphabets
Newsgroups: gmane.comp.lang.ruby.rails
Date: 2008-07-22 23:00:49 GMT
Subject: Re: How to detect if a string contains any funny characters from non English alphabets
Newsgroups: gmane.comp.lang.ruby.rails
Date: 2008-07-22 23:00:49 GMT
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)
RSS Feed