Solved

basic reg ex splitting a name into forename and surname

Posted on 2007-12-02
6
564 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now