Regular Expression Problem

  • Thread starter Thread starter John Lorenzen
  • Start date Start date
J

John Lorenzen

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
 
John Lorenzen said:
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(',');
 
John Lorenzen said:
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
 
Thomas T. Veldhouse said:
John Lorenzen said:
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.
 
This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following case

Washington,George

I was hoping for
Washington
George
 
John Lorenzen said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top