473,480 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1774
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
3131
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
5348
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
3198
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
3791
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
3336
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
3312
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
5128
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
3774
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
2089
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
1657
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
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7045
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
7087
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...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.