By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,563 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,563 IT Pros & Developers. It's quick & easy.

Regular Expression Problem

P: n/a
Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string
Aug 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
John Lorenzen <jo***@fidlar.comwrote:
Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string
You could use Regex for this if you like, but it seems easier to use
String.Split()

string[] name = georgesName.Split(',');

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 16 '06 #2

P: n/a
"John Lorenzen" <jo***@fidlar.comwrote in message
news:us*************@TK2MSFTNGP04.phx.gbl...
Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string

How about

^(.+),(.+?)\s+(.+)$
Results for these input:

Washington,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George W."
Group 2 = "Washington"
Group 3 = "George"
Group 4 = "W."

Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."

-- Alan
Aug 16 '06 #3

P: n/a
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
John Lorenzen <jo***@fidlar.comwrote:
>Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string

You could use Regex for this if you like, but it seems easier to use
String.Split()

string[] name = georgesName.Split(',');
My bad ... I didn't read thoroughly to realize that you wanted the middle
initial or name grouped as well. Regex is definitely the proper solution in
that case. I see another poster posted a regex recipe, so perhaps that is
what you are looking for.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 16 '06 #4

P: n/a
This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following case

Washington,George

I was hoping for
Washington
George

"Alan Pretre" <no@spamwrote in message
news:uS**************@TK2MSFTNGP05.phx.gbl...
"John Lorenzen" <jo***@fidlar.comwrote in message
news:us*************@TK2MSFTNGP04.phx.gbl...
>Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string


How about

^(.+),(.+?)\s+(.+)$
Results for these input:

Washington,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George W."
Group 2 = "Washington"
Group 3 = "George"
Group 4 = "W."

Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."

-- Alan


Aug 17 '06 #5

P: n/a
"John Lorenzen" <jo***@fidlar.comwrote in message
news:eM**************@TK2MSFTNGP02.phx.gbl...
This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following case

Washington,George

I was hoping for
Washington
George

OK.
^(.+),(.+?)(?:\s+(.+))*$
Washington,George
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George"
Group 2 = "Washington"
Group 3 = "George"
Group 4 = ""
Bush,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "W."
Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."


-- Alan
Aug 17 '06 #6

P: n/a
Thanks Alan, could you recommend a good Regular Expression book?

"Alan Pretre" <no@spamwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
"John Lorenzen" <jo***@fidlar.comwrote in message
news:eM**************@TK2MSFTNGP02.phx.gbl...
>This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following
case

Washington,George

I was hoping for
Washington
George


OK.
^(.+),(.+?)(?:\s+(.+))*$
Washington,George
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George"
Group 2 = "Washington"
Group 3 = "George"
Group 4 = ""
Bush,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "W."
Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."


-- Alan


Aug 17 '06 #7

P: n/a
"John Lorenzen" <jo***@fidlar.comwrote in message
news:OF**************@TK2MSFTNGP03.phx.gbl...
Thanks Alan, could you recommend a good Regular Expression book?
I think the O'Reilly books are the best, at least for the Unix world, which
is where RegExp originates.

You may prefer a book tailored specifically for .NET, though.

http://www.amazon.com/s/ref=nb_ss_b/...Go.x=18&Go.y=2

-- Alan


Aug 17 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.