Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regexp to split name?

Reply
Thread Tools

Regexp to split name?

 
 
Alex MacCaw
Guest
Posts: n/a
 
      06-23-2007
Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
darren kirby
Guest
Posts: n/a
 
      06-23-2007
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

 
Reply With Quote
 
 
 
 
dblack@wobblini.net
Guest
Posts: n/a
 
      06-23-2007
Hi --

On Sun, 24 Jun 2007, 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...


And also last names with spaces in them (von Trapp, Vaughn Williams,
etc.).


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      06-25-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi --
>
> On Sun, 24 Jun 2007, 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...

>
> And also last names with spaces in them (von Trapp, Vaughn Williams,
> etc.).
>

And titles with spaces in them (The Honourable, His Excellency, etc...).

--
Alex

 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      06-25-2007
On 6/25/07, Alex Young <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Hi --
> >
> > On Sun, 24 Jun 2007, 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...

> >
> > And also last names with spaces in them (von Trapp, Vaughn Williams,
> > etc.).
> >

> And titles with spaces in them (The Honourable, His Excellency, etc...).
>


And international names (though the US seems to have a broad
assortment of them already)

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      06-25-2007
Michael Fellinger wrote:
> On 6/25/07, Alex Young <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>> > Hi --
>> >
>> > On Sun, 24 Jun 2007, 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...
>> >
>> > And also last names with spaces in them (von Trapp, Vaughn Williams,
>> > etc.).
>> >

>> And titles with spaces in them (The Honourable, His Excellency, etc...).
>>

>
> And international names (though the US seems to have a broad
> assortment of them already)
>

Can open. Worms everywhere.

--
Alex

 
Reply With Quote
 
Dan Stevens (IAmAI)
Guest
Posts: n/a
 
      06-25-2007
On 23/06/07, darren kirby <(E-Mail Removed)> 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"

 
Reply With Quote
 
dblack@wobblini.net
Guest
Posts: n/a
 
      06-25-2007
Hi --

On Mon, 25 Jun 2007, Dan Stevens (IAmAI) wrote:

> 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


Have mercy on us Yanks and allow for a period

> first = a.shift
> middles = a
>
> title #=> "Mr"
> first #=> "John"
> middles #=> ["Joe", "Peter"]
> last #=> Smith"


However:

name = "Mr Andrew Lloyd Webber"

# etc.

title #=> "Mr"
first #=> "Andrew"
middles #=> ["Lloyd"] (wrong)
last #=> Webber" (wrong)


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      06-25-2007
(E-Mail Removed) wrote:
> Hi --
>
> On Mon, 25 Jun 2007, Dan Stevens (IAmAI) wrote:
>
>> 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

>
> Have mercy on us Yanks and allow for a period
>
>> first = a.shift
>> middles = a
>>
>> title #=> "Mr"
>> first #=> "John"
>> middles #=> ["Joe", "Peter"]
>> last #=> Smith"

>
> However:
>
> name = "Mr Andrew Lloyd Webber"
>
> # etc.
>
> title #=> "Mr"
> first #=> "Andrew"
> middles #=> ["Lloyd"] (wrong)
> last #=> Webber" (wrong)
>


name = "The Honourable Lord Andrew, the Baron Lloyd-Webber of
Sydmonton", you mean? It's hard to come up with a trickier example.
Names are just *hard* - the only reliable way of handling them that I've
found is to let users control it themselves...

--
Alex

 
Reply With Quote
 
Dan Stevens (IAmAI)
Guest
Posts: n/a
 
      06-25-2007
> Names are just *hard* - the only reliable way of handling them that I've
> found is to let users control it themselves...


Agreed. My example makes very simple assumptions that I'd imagine
apply to the vast majority of names. However, in many computer
problems there are obscure exceptions that either break the program or
break things for the user.

On 25/06/07, Alex Young <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Hi --
> >
> > On Mon, 25 Jun 2007, Dan Stevens (IAmAI) wrote:
> >
> >> 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

> >
> > Have mercy on us Yanks and allow for a period
> >
> >> first = a.shift
> >> middles = a
> >>
> >> title #=> "Mr"
> >> first #=> "John"
> >> middles #=> ["Joe", "Peter"]
> >> last #=> Smith"

> >
> > However:
> >
> > name = "Mr Andrew Lloyd Webber"
> >
> > # etc.
> >
> > title #=> "Mr"
> > first #=> "Andrew"
> > middles #=> ["Lloyd"] (wrong)
> > last #=> Webber" (wrong)
> >

>
> name = "The Honourable Lord Andrew, the Baron Lloyd-Webber of
> Sydmonton", you mean? It's hard to come up with a trickier example.
> Names are just *hard* - the only reliable way of handling them that I've
> found is to let users control it themselves...
>
> --
> Alex
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments