[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

basic reg ex splitting a name into forename and surname

Posted on 2007-12-02
6
Medium Priority
?
574 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 48

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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 Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
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…

656 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