From: "Dan Stevens (IAmAI)" Date: 2007-06-25T22:01:23+09:00 Subject: Re: Regexp to split name? On 23/06/07, darren kirby wrote: > quoth the Alex MacCaw: > > Does anyone have an example of splitting a name into first and last > > names? Or is just a case of doing string.split(' ')? > > I'd say a regexp is overkill here. > > irb(main):001:0> name = "Alex MacCaw" > => "Alex MacCaw" > irb(main):002:0> first, last = name.split > => ["Alex", "MacCaw"] > irb(main):003:0> first > => "Alex" > irb(main):004:0> last > => "MacCaw" > > Note that you will have to do more work to accommodate middle names and > titles, ie: Mr, Mrs, Dr etc... > > -d > -- > darren kirby :: Part of the problem since 1976 :: http://badcomputer.org > "...the number of UNIX installations has grown to 10, with more expected..." > - Dennis Ritchie and Ken Thompson, June 1972 > > name = "Mr John Joe Peter Smith" TITLES = ["Mr", "Mrs", "Ms", "Dr"] a = name.split last = a.pop title = a.shift if TITLES.include? a.first first = a.shift middles = a title #=> "Mr" first #=> "John" middles #=> ["Joe", "Peter"] last #=> Smith"