[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp to split name?

Alex MacCaw

6/23/2007 3:55:00 PM

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-....

14 Answers

darren kirby

6/23/2007 4:02:00 PM

0

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://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

dblack

6/23/2007 4:05:00 PM

0

Alex Young

6/25/2007 7:53:00 AM

0

dblack@wobblini.net 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

Michael Fellinger

6/25/2007 8:40:00 AM

0

On 6/25/07, Alex Young <alex@blackkettle.org> wrote:
> dblack@wobblini.net 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)

Alex Young

6/25/2007 9:12:00 AM

0

Michael Fellinger wrote:
> On 6/25/07, Alex Young <alex@blackkettle.org> wrote:
>> dblack@wobblini.net 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

Dan Stevens (IAmAI)

6/25/2007 1:01:00 PM

0

On 23/06/07, darren kirby <bulliver@badcomputer.org> 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://badco...
> "...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"

dblack

6/25/2007 1:37:00 PM

0

Alex Young

6/25/2007 1:51:00 PM

0

dblack@wobblini.net 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

Dan Stevens (IAmAI)

6/25/2007 3:38:00 PM

0

> 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 <alex@blackkettle.org> wrote:
> dblack@wobblini.net 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
>
>

Alex LeDonne

6/25/2007 5:32:00 PM

0

On 6/25/07, Dan Stevens (IAmAI) <dan.stevens.iamai@gmail.com> wrote:
> On 25/06/07, Alex Young <alex@blackkettle.org> wrote:
> > 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.

I worked at an institution that was forced to rewrite a bunch of
name-related code for a legacy system because of a "sanity" check that
was just plain wrong... and nobody realized it until Dr. O came to
work. Now they had to allow one-letter surnames, too (they'd already
allowed one-letter given or middle names, thanks to President Truman's
middle name, S).

Almost any assumption you make about name parsing will be wrong. For
example, take the assumption that names are composed only of letters
and letter-like symbols.

http://en.wikipedia.org/wiki/Jenni...
http://en.wikipedia.org/wiki/Nancy_...
http://en.wikipedia.org/wiki/List_of_personal_names_that_conta...

-Alex