Forum: Ruby Regexp to split name?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Baf018e2cc4616e4776d323215c7136c?d=identicon&s=25 Alex MacCaw (Guest)
on 2007-06-23 17:54
Does anyone have an example of splitting a name into first and last
names? Or is just a case of doing string.split(' ')?
0cb023654f46a6bd171405af6419baf6?d=identicon&s=25 darren kirby (Guest)
on 2007-06-23 18:03
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-23 18:06
(Received via mailing list)
Hi --

On Sun, 24 Jun 2007, darren kirby wrote:

> 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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-06-25 09:53
(Received via mailing list)
dblack@wobblini.net wrote:
>> irb(main):001:0> name = "Alex MacCaw"
>
> And also last names with spaces in them (von Trapp, Vaughn Williams,
> etc.).
>
And titles with spaces in them (The Honourable, His Excellency, etc...).
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2007-06-25 10:40
(Received via mailing list)
On 6/25/07, Alex Young <alex@blackkettle.org> wrote:
> >>
> >> 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)
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-06-25 11:13
(Received via mailing list)
Michael Fellinger wrote:
>> >> I'd say a regexp is overkill here.
>> >> Note that you will have to do more work to accommodate middle names
> assortment of them already)
>
Can open.  Worms everywhere.  :-)
2b4da3f15e2d0f58be623bf40795de07?d=identicon&s=25 Dan Stevens (IAmAI) (Guest)
on 2007-06-25 15:03
(Received via mailing list)
On 23/06/07, darren kirby <bulliver@badcomputer.org> wrote:
> irb(main):003:0> first
> "...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"
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-25 15:39
(Received via mailing list)
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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-06-25 15:52
(Received via mailing list)
dblack@wobblini.net wrote:
> Have mercy on us Yanks and allow for a period :-)
>
>   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...
2b4da3f15e2d0f58be623bf40795de07?d=identicon&s=25 Dan Stevens (IAmAI) (Guest)
on 2007-06-25 17:38
(Received via mailing list)
> 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.
05e48e632fdd0b2c25d27042f52c11d5?d=identicon&s=25 Alex LeDonne (Guest)
on 2007-06-25 19:34
(Received via mailing list)
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/Jennifer_8._Lee
http://en.wikipedia.org/wiki/Nancy_3._Hoffman
http://en.wikipedia.org/wiki/List_of_personal_name...

-Alex
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2007-06-25 22:58
(Received via mailing list)
Alex LeDonne wrote:
> 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.

Not to mention the assumption that each name consists of symbols that
are part of some character set:

http://upload.wikimedia.org/wikipedia/commons/thum...
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-06-27 20:36
(Received via mailing list)
On 6/25/07, Alex LeDonne <aledonne.listmail@gmail.com> wrote:

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

Reminds me of an old SF story "The Man Whose Name Wouldn't Fit."  IIRC
it started with a guy getting fired because his company put in a new
computer personnel data system which had one too many characters in
the field for last name to accomodate him (and it was too expensive to
fix).

I think it ended with a neo-luddite movement with a secret weapon
which dissolved the bond between the the magnetic material and the
substrate on magnetic tapes and disks.

Don't know how many here are old enough to remember when most
computers used magnetic tape. <G>

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-06-27 20:47
(Received via mailing list)
Hi --

On Thu, 28 Jun 2007, Rick DeNatale wrote:

> it started with a guy getting fired because his company put in a new
> computer personnel data system which had one too many characters in
> the field for last name to accomodate him (and it was too expensive to
> fix).
>
> I think it ended with a neo-luddite movement with a secret weapon
> which dissolved the bond between the the magnetic material and the
> substrate on magnetic tapes and disks.
>
> Don't know how many here are old enough to remember when most
> computers used magnetic tape. <G>

I sometimes wonder whether the DECtapes in my attic would still be
readable.


David
Ee04bc0ca6dcdad4a7e8a8e1d4efb5d0?d=identicon&s=25 Michael W. Ryder (Guest)
on 2007-06-27 20:56
(Received via mailing list)
Rick DeNatale wrote:
> it started with a guy getting fired because his company put in a new
> computer personnel data system which had one too many characters in
> the field for last name to accomodate him (and it was too expensive to
> fix).
>
I assume you meant that his name was one character too long, not that
the field for the name was too long.

> I think it ended with a neo-luddite movement with a secret weapon
> which dissolved the bond between the the magnetic material and the
> substrate on magnetic tapes and disks.
>
> Don't know how many here are old enough to remember when most
> computers used magnetic tape. <G>
>
I remember carrying boxes of punched cards.  One of my previous bosses
told me a humorous horror story about a company he worked for.  The
company magazine was doing an article on the Data Processing department
and wanted a picture of the computers at work.  The department decided
that the best time would be during the payroll run when all of the tape
drives would be in use.  They set up the shot and when the flash went
off all of the tape drives went off line.  Everyone had forgotten about
the optical EOT sensors.
This topic is locked and can not be replied to.