List Info

Thread: Separating first and last names with optional suffix




Separating first and last names with optional suffix
country flaguser name
United States
2007-06-26 14:19:45
I'm trying to write a regex for extracting surnames and
given names
from names that look like this

John Jacob Doe Jr.

I'd like $1 to contain "John Jacob" and $2 to
contain "Doe" and the
optional Jr/Sr suffix to be ignored.

(.*?) ([^ ]+)(?: +(?:Sr|Jr).?)? doesn't work.  Any ideas?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Separating first and last names with optional suffix
country flaguser name
United States
2007-06-26 14:25:10
I figured it out:

/^(.*?) ([^ ]+)(?: +(?:Sr|Jr).?)?$/i

On Jun 26, 1:19 pm, russ <black.russ...gmail.com> wrote:
> I'm trying to write a regex for extracting surnames and
given names
> from names that look like this
>
> John Jacob Doe Jr.
>
> I'd like $1 to contain "John Jacob" and $2 to
contain "Doe" and the
> optional Jr/Sr suffix to be ignored.
>
> (.*?) ([^ ]+)(?: +(?:Sr|Jr).?)? doesn't work.  Any
ideas?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Separating first and last names with optional suffix
country flaguser name
United States
2007-06-26 14:47:28
I would use this regex

^s*(w+s+w+)s+(w+)

$1 will contain the first two words on the line
$2 will contain the third word on the line

Leading and trailing white spaces are allowed

If it is important that there are no further words except an
optional
Jr. or Sr. do this:

^s*(w+s+w+)s+(w+)s*(s[JS]r.s*)?$

Admittedly, I haven't tested the two regexes

Greetings

Flo


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Separating first and last names with optional suffix
country flaguser name
United States
2007-06-26 16:54:35
Thanks for the quick response, Flo.  I actually needed an
expression
that handled an arbitrary number of given names, although my
example
just contained two.  I was able to do it with

/^(.*?) ([^ ]+)(?: +(?:Sr|Jr).?)?$/i

This list is a great idea.  There's no one here at work
that
understands regex's better than me so when I need help it's
nice to
have a place to go.

On Jun 26, 1:47 pm, Flo <sensor...gmail.com> wrote:
> I would use this regex
>
> ^s*(w+s+w+)s+(w+)
>
> $1 will contain the first two words on the line
> $2 will contain the third word on the line
>
> Leading and trailing white spaces are allowed
>
> If it is important that there are no further words
except an optional
> Jr. or Sr. do this:
>
> ^s*(w+s+w+)s+(w+)s*(s[JS]r.s*)?$
>
> Admittedly, I haven't tested the two regexes
>
> Greetings
>
> Flo


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )