Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Regular Expression Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Lorenzen - 16 Aug 2006 20:21 GMT
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
Thomas T. Veldhouse - 16 Aug 2006 20:26 GMT
> 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

Alan Pretre - 16 Aug 2006 20:40 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.