C
Chris Dunaway
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}"
MsgBox(sResult)
End If
End Sub
Can anyone help me with this Regular Expression? They can be very
confusing.
Can you point me to any web sites or other resources on RegExp?
Thanks,
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}"
MsgBox(sResult)
End If
End Sub
Can anyone help me with this Regular Expression? They can be very
confusing.
Can you point me to any web sites or other resources on RegExp?
Thanks,