?
Solved

basic reg ex splitting a name into forename and surname

Posted on 2007-12-02
6
Medium Priority
?
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1400 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 600 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
Independent Software Vendors: 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

771 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