From: Shandy Nantz Date: 2008-11-14T07:49:46+09:00 Subject: Re: Matching Peter Szinek wrote: > Here is a 'full' solution that should cover these cases: > > >> "Colorado Springs, CO[COS]"[/(\w|\s)+, [A-Z]{2}\[(.+?)\]/,2] > => "COS" > >> "Denver, CO[DEN]"[/(\w|\s)+, [A-Z]{2}\[(.+?)\]/,2] > => "DEN" I have had to refine my search a little, mostly because getting the last four characters turned out not to be a simple thing when using regular expressions. I have people entering text via an auto_complete_text_field in a rails app; my issues is that on my local machine there parameters get passed like this: Denver, CO[DEN] or Colorado Springs, CO[COS] However, up on the server the parameters get passed like this: " Denver, CO[DEN] " or " Colorado Springs, CO[COS] " and for whatever reason (my guess being because regex is a pain in the a#%), simply replacing the spaces dident work and cause massive amounts of havoic. So I used the code found up above and all was well in the world.... Then a new hick-up was encountered: Toronto, Canada[YYZ] As you can imagine the regex wouldn't ketch this expression. So I tried this: params[:airport][:city].gsub(/$\s/, "") [/(\w|\s)+, [A-Z]{2}|[A-Z][a-z]{1,}\[(.+?)\]/,2] And al' bee it caught the Torono, Canada[YYZ]. Now, however, it fails to get the Denver, CO[DEN]. I must have a complete lack of understanding when it comes to regex because I always thought that the "|" in the above statement meant "or", as in, try and match TWO A-Z OR one A-Z FOLLOWED BY one or more a-z. I guess I am drawing a total blank because I look at the revised code above and I think it should match all the above cases and I cannot figure out for the life of me where it is failing. Can someon ehelp, thanks -S -- Posted via http://www.ruby-forum.com/.