Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if string contains only latin symbols using Ruby 1.9?

Tags:

regex

ruby

ascii

I need to detect if some string contains symbols from a non latin alphabet. Numbers and special symbols like -, _, + are good. I need to know whether there is any non latin symbols. For example:

"123sdjjsf-4KSD".just_latin?

should return true.

"12333ыц4--sdf".just_latin?

should return false.

like image 406
user1859243 Avatar asked Dec 02 '12 16:12

user1859243


1 Answers

I think that this should work for you:

 # encoding: UTF-8

 class String
   def just_latin?
     !!self.match(/^[a-zA-Z0-9_\-+ ]*$/)
   end
 end

 puts "123sdjjsf-4KSD".just_latin?
 puts "12333ыц4--sdf".just_latin?

Note that *#ascii_only?* is very close to what you want as well.

like image 65
G. Allen Morris III Avatar answered Nov 15 '22 20:11

G. Allen Morris III