RegEx to match names?

Discussion in 'Perl Misc' started by Patrick D., Aug 21, 2003.

  1. Patrick D.

    Patrick D. Guest

    There are a few different name formats out there, and I'm having trouble
    incorporating them into one regex.

    e.g.

    George Walker Bush
    George W. Bush
    George Bush
    G. Walker Bush
    G. W. Bush
    G.W. Bush

    What I want is a way to always be able to identify the first+middle and
    last names. I'd like the $1 to be "George Walker" or "George W." or G.
    Walker", or just "George" if there is no middle. With $2, hopefully it
    could contain *only* "Bush".

    Can anyone help me make a regex for all the above?
    Patrick D., Aug 21, 2003
    #1
    1. Advertising

  2. Patrick D.

    Sam Holden Guest

    On Thu, 21 Aug 2003 05:09:43 GMT, Patrick D. <> wrote:
    > There are a few different name formats out there, and I'm having trouble
    > incorporating them into one regex.
    >
    > e.g.
    >
    > George Walker Bush
    > George W. Bush
    > George Bush
    > G. Walker Bush
    > G. W. Bush
    > G.W. Bush
    >
    > What I want is a way to always be able to identify the first+middle and
    > last names. I'd like the $1 to be "George Walker" or "George W." or G.
    > Walker", or just "George" if there is no middle. With $2, hopefully it
    > could contain *only* "Bush".
    >
    > Can anyone help me make a regex for all the above?


    /(.*)\s+(\S+)/;

    For something more robust you could try a module like Lingua::En:NameParse.

    --
    Sam Holden
    Sam Holden, Aug 21, 2003
    #2
    1. Advertising

  3. Patrick D.

    Sam Holden Guest

    On 21 Aug 2003 05:25:52 GMT, Sam Holden <> wrote:
    >
    > /(.*)\s+(\S+)/;


    The + in \s+ might as well be dropped, since if there are multiple
    spaces, the .* is going to grab them leaving just one for the \s+ anyway.

    Or the .* could be replaced with .*?, if trailing spaces in $1 aren't
    wanted.

    --
    Sam Holden
    Sam Holden, Aug 21, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    636
  2. Victor
    Replies:
    2
    Views:
    643
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    369
    ekzept
    Aug 10, 2007
  4. Replies:
    3
    Views:
    757
    Reedick, Andrew
    Jul 1, 2008
  5. Brian D
    Replies:
    5
    Views:
    2,839
Loading...

Share This Page