By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Help with RegExp

P: n/a
I have a string: 555-1234,12345,"Jones, John"

I want to split the string based on a comma as a delimiter. Since the name
portion has a comma and is delimited with quotation marks, I need to ignore
or strip them out. I also want to reverse the last and first names so that
"Jones, John" becomes John Jones (no quotation marks or commas, last and
first name reversed).

I thought, a regular expression should do the trick. So I came with the
following regular expression: \""+(?<Last>.*),*(?<First>.*)" & "\""+

This regular expression works in that it correctly finds the name portion
of the string. I then attempt to do a replacement as follows (rx is a
RegExp instance using the expression above):

sResult = rx.Replace(sSource,"${First} ${Last}")

The result string is the same as the source string except that the
quotation marks have been removed (which is correct) but the comma still
remains and the names are not reversed. Upon further investigation, it
appears that the ${Last} sub part includes the Last name, the comma, and
the first name and the ${First} sub part doesn't contain anything!

Here is the complete code:

Private Sub Button1_Click(...) Handles Button1.Click
Dim sExp As String = "\""+(?<Last>.*),*(?<First>.*)" & "\""+"
Dim rx As New Regex(sExp)
Dim sSource As String = "555-1234,12345,""Jones, John"""
Dim sResult As String

If rx.IsMatch(sSource) Then
sResult = rx.Replace(sSource,"${First} ${Last}"
End If
End Sub

Can anyone help me with this Regular Expression? They can be very

Can you point me to any web sites or other resources on RegExp?



To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
On Tue, 27 Jan 2004 11:08:26 -0600, Chris Dunaway wrote:
following regular expression: \""+(?<Last>.*),*(?<First>.*)" & "\""+

Ok, I figured out the problem. The problem is in using the .* for find
characters. That find *any* character, including a comma. So I changed
the RegExp to this: \x22+(?<Last>[^,]*)[, ]*(?<First>[^,]*)\x22+

and now it works correctly.

Hope this helps others


To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.