Solved

Extract column values from combine string  data

Posted on 2013-12-07
11
212 Views
Last Modified: 2013-12-12
Dear Experts,

I have these common data patterns (City, State Zip) as Location, and I want them to split into 3 accordingly using regex and /or substring combination under VB.Net environment:

The actual data looks like as follows:

Atlanta, GA 30315    
to break into:

Atlanta
GA
30315

Kansas City, KS 66016
to break into:

Kansas City
KS
66016


Thanks








Best solution would be greatly appreciated.


Thanks
0
Comment
Question by:JimiJ13
[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
  • 5
  • 5
11 Comments
 
LVL 51

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 100 total points
ID: 39702923
Hi

pls try

strAddress = "Atlanta, GA 30315"

str1 = Split(strAddress, ",")(0)
str2 = Split(Trim(Split(strAddress, ",")(1)), " ")(0)
str3 = Split(Trim(Split(strAddress, ",")(1)), " ")(1)

Open in new window

Regards
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39703101
Hi JimiJ13;

Here is sample code using regular expression to parse the Location string.

' Locations to be parsed
Dim Locations As New List(Of String)() From { "Atlanta, GA 30315", "Kansas City, KS 66016" }
' The regular expression with named groups
Dim pattern As String = "(?<City>[^,]+),\s+(?<State>[^\s]+)\s+(?<Zip>\d+)"

' Get each individual location
For Each Location In Locations
    ' Find the individual locations
    Dim match As Match = Regex.Match(Location, pattern)
    ' Access the three members by using group names
    Console.WriteLine("City = {0}  :  State = {1}  :  Zip = {2}", _
        match.Groups("City").Value, _
        match.Groups("State").Value, _
        match.Groups("Zip").Value )
Next

Open in new window

0
 

Author Comment

by:JimiJ13
ID: 39703521
Hi Rgonzo1971,

Your suggestion works great for a perfect data pattern. However, I see some data with extra spaces between values that resulted to inaccurate outputs.

Any idea how to deal with it?  


Thanks.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39703525
Can you give an example of, " I see some data with extra spaces between values that resulted to inaccurate outputs. ", so that I may try and adjust the pattern.
0
 

Assisted Solution

by:JimiJ13
JimiJ13 earned 0 total points
ID: 39703528
Hi  FernandoSoto,

Your solution works. Nevertheless, since this will be applied at one data at a time in the Add/Edit page, I just thought that a more specific for a single-line  record method must be more efficient.

What do you think?

Thanks.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 39703541
Hi JimiJ13;

As long as you define the pattern string before using the regex this is all that is needed.

Dim match As Match = Regex.Match(Location, pattern)

Location in the statement is the instance of the string to be parsed and match holds the parsed data. That is a one liner.
0
 

Author Comment

by:JimiJ13
ID: 39703571
Hi Fernando,

The unusual pattern that I saw is something like this:

Kansas City  , KS   66016


Thanks.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39703653
Because the string may not be in the correct format we will reformat the string before using it. Before using the Location variable in the regex do this first and everything else stays the same.

Dim Location As String = Regex.Replace("Kansas City  , KS   66016", "\s{2,},", ",")

Of course you will replace the string "Kansas City  , KS   66016" with the variable of the string needing to be parsed.
0
 

Author Comment

by:JimiJ13
ID: 39705543
Hi Fernando,

On the actual implementation of your solution, I got error that says:

"Location is not declared or may be inaccessible due to Protection Level"


Any idea?
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39705898
Hi JimiJ13;

Location is just the variable name I used to represent the data to be parsed, just change it to what you are using to hold the string to be parsed.
0
 

Author Closing Comment

by:JimiJ13
ID: 39713686
Fernando has the great solution!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Groupbox Control ? 2 39
visual studio vb.net windows onkeyup 2 40
"lblTime is not declared" 3 65
Subtract dates in vb.net 6 33
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 …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

732 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