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!
Regards,
David Alves
On Jul 22, 2:04 pm, Chris Nowlan <chrisn...@...> wrote:
> Hello!
>
> I have a specific problem that maybe you can help with.
>
> Given an input of string (up to 1000 characters), how do I detect if
> the string is not written in "Roman Alphabets (A-Z)", and by that I mean
> that the string maybe in Chinese, Korean, or contain non-standard
> English or western language alphabets, etc.
>
> I just need a function that will return "True" or "False".
>
> Thanks in advance!
>
> -Chris
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe <at> googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
RSS Feed