473,385 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regular Expression Problem

Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string
Aug 16 '06 #1
7 1768
John Lorenzen <jo***@fidlar.comwrote:
Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string
You could use Regex for this if you like, but it seems easier to use
String.Split()

string[] name = georgesName.Split(',');

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 16 '06 #2
"John Lorenzen" <jo***@fidlar.comwrote in message
news:us*************@TK2MSFTNGP04.phx.gbl...
Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string

How about

^(.+),(.+?)\s+(.+)$
Results for these input:

Washington,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George W."
Group 2 = "Washington"
Group 3 = "George"
Group 4 = "W."

Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."

-- Alan
Aug 16 '06 #3
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
John Lorenzen <jo***@fidlar.comwrote:
>Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string

You could use Regex for this if you like, but it seems easier to use
String.Split()

string[] name = georgesName.Split(',');
My bad ... I didn't read thoroughly to realize that you wanted the middle
initial or name grouped as well. Regex is definitely the proper solution in
that case. I see another poster posted a regex recipe, so perhaps that is
what you are looking for.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Aug 16 '06 #4
This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following case

Washington,George

I was hoping for
Washington
George

"Alan Pretre" <no@spamwrote in message
news:uS**************@TK2MSFTNGP05.phx.gbl...
"John Lorenzen" <jo***@fidlar.comwrote in message
news:us*************@TK2MSFTNGP04.phx.gbl...
>Given the following string Washington,George W.
how can I use regular expression to extract the parts of the name out
lastname should be from the start of the string till the comma
firstname should be from the comma to the space
middle name from the space to the end to the string


How about

^(.+),(.+?)\s+(.+)$
Results for these input:

Washington,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George W."
Group 2 = "Washington"
Group 3 = "George"
Group 4 = "W."

Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."

-- Alan


Aug 17 '06 #5
"John Lorenzen" <jo***@fidlar.comwrote in message
news:eM**************@TK2MSFTNGP02.phx.gbl...
This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following case

Washington,George

I was hoping for
Washington
George

OK.
^(.+),(.+?)(?:\s+(.+))*$
Washington,George
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George"
Group 2 = "Washington"
Group 3 = "George"
Group 4 = ""
Bush,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "W."
Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."


-- Alan
Aug 17 '06 #6
Thanks Alan, could you recommend a good Regular Expression book?

"Alan Pretre" <no@spamwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
"John Lorenzen" <jo***@fidlar.comwrote in message
news:eM**************@TK2MSFTNGP02.phx.gbl...
>This ^(.+),(.+?)\s+(.+)$ pattern works great except for the following
case

Washington,George

I was hoping for
Washington
George


OK.
^(.+),(.+?)(?:\s+(.+))*$
Washington,George
1 matches.
Match 1 has 4 groups.
Group 1 = "Washington,George"
Group 2 = "Washington"
Group 3 = "George"
Group 4 = ""
Bush,George W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "W."
Bush,George H. W.
1 matches.
Match 1 has 4 groups.
Group 1 = "Bush,George H. W."
Group 2 = "Bush"
Group 3 = "George"
Group 4 = "H. W."


-- Alan


Aug 17 '06 #7
"John Lorenzen" <jo***@fidlar.comwrote in message
news:OF**************@TK2MSFTNGP03.phx.gbl...
Thanks Alan, could you recommend a good Regular Expression book?
I think the O'Reilly books are the best, at least for the Unix world, which
is where RegExp originates.

You may prefer a book tailored specifically for .NET, though.

http://www.amazon.com/s/ref=nb_ss_b/...Go.x=18&Go.y=2

-- Alan


Aug 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Harry | last post by:
Hi there, does anyone know how I can build a regular expression e.g. for the string.search() function on runtime, depending on the content of variables? Should be something like this: var...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
9
by: Pete Davis | last post by:
I'm using regular expressions to extract some data and some links from some web pages. I download the page and then I want to get a list of certain links. For building regular expressions, I use...
3
by: LordHog | last post by:
Hello all, I am attempting to create a small scripting application to be used during testing. I extract the commands from the script file I was going to tokenize the each line as one of the...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
5
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
1
by: sunil | last post by:
Hi, Am writing one C program for one of my module and facing one problem with the regular expression functions provided by the library libgen.h in solaris. In this library we are having two...
1
by: Shawn B. | last post by:
Greetings, I'm using a custom WebBrowser control: http://www.codeproject.com/KB/miscctrl/csEXWB.aspx When I get the DocumentSource of a web page I browsed, and run a regular expression...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.