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
> 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(',');

Signature
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Thomas T. Veldhouse - 16 Aug 2006 20:50 GMT
>> Given the following string Washington,George W.
>> how can I use regular expression to extract the parts of the name out
[quoted text clipped - 6 lines]
>
> 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.

Signature
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
> 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
John Lorenzen - 17 Aug 2006 22:11 GMT
This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following case
Washington,George
I was hoping for
Washington
George
>> Given the following string Washington,George W.
>> how can I use regular expression to extract the parts of the name out
[quoted text clipped - 25 lines]
>
> -- Alan
Alan Pretre - 17 Aug 2006 22:21 GMT
> This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following case
>
[quoted text clipped - 3 lines]
> 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
John Lorenzen - 17 Aug 2006 22:47 GMT
Thanks Alan, could you recommend a good Regular Expression book?
>> This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following
>> case
[quoted text clipped - 34 lines]
>
> -- Alan
Alan Pretre - 17 Aug 2006 23:09 GMT
> 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/102-9426835-7225700?url=search-alias%3Dstrip
books&field-keywords=regular+expressions&Go.x=18&Go.y=2
-- Alan