Solved

basic reg ex splitting a name into forename and surname

Posted on 2007-12-02
6
568 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

685 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