Solved

basic reg ex splitting a name into forename and surname

Posted on 2007-12-02
6
566 Views
Last Modified: 2012-05-05
Hi

I don't know very much about regular expressions. How would i split a string input that represents a name into its forname and surname parts. I am assuming forenames cannot contain spaces for now so i would need to split the string into 2 parts based on the first space character in the string input. I then need each part of the regular expression in 2 separate variables. I think i would use the Match object for this but i dont know how to access the matched parts.

thanks
andrea
0
Comment
Question by:andieje
6 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20392291
Hi Andrea,

You need not use RegExp for this, as the String object has a Split method, which returns an array of strings, using the specified delimiter....

    Dim FullName() As String = "John Smith".Split(" ")
    Dim FirstName As String = FullName(0)
    Dim Surname As String = FullName(1)

Regards,

Wayne
0
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 350 total points
ID: 20392578
You could just locate where the space is in the string, and use standard string operations to get the first and last name:

Dim name As String = "Peter Parker"
Dim index As Integer = name.IndexOf(" "C)
Dim FirstName As String = name.Substring(0, index)
Dim LastName As String = name.Substring(index + 1)

If you want to split on the last space in the string instead, use LastIndexOf instead of IndexOf.
0
 
LVL 17

Assisted Solution

by:ZeonFlash
ZeonFlash earned 150 total points
ID: 20392807
Since your question states that you need the answer in the form of a regex, I've provided one below which you can use to get the first and last names.  My knee-jerk reaction is to use basic string manipulation like the above posted comments, but you come into trouble when running across names like "Bill Von Trapp", where Von Trapp is the full last name.  Using a regex can allow you access the names via the "groups" property of a regex Match.
^(?<First>[^ ]+)[ ](?<Last>.+)$

Open in new window

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:andieje
ID: 20471873
hi webtubbs

won't that solution split a surname that includes a space in it too? i think green shost has provided a more accurate solution

thanks a lot
0
 

Author Closing Comment

by:andieje
ID: 31427363
thanks a lot
0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20472539
> won't that solution split a surname that includes a space in it too?

Yes, you would only get the first and middle name:

Dim FullName() As String = "Georg von Trapp".Split(" ")

FullName(0) contains "Georg"
FullName(1) contains "von"

but:

FullName(2) contains "Trapp"

It's possible to use the split without losing the information, you can limit the split to not return more than two strings:

Dim FullName() As String = "Georg von Trapp".Split(" ".ToCharArray(), 2)

FullName(0) contains "Georg"
FullName(1) contains "von Trapp"


However, if you have a string containing more than strictly a first and last name, you can't really split it accurately if you don't have any information what the middle part is. It can be who first names like "Reginald Kenneth Dwight", a nickname like "Bernard 'Bernie' Taupin", a split last name like "Jon Bon Jovi", a title like "Georg von Trapp" or a double last name like "Frank Lloyd Wright".
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question