I am trying to make a VB.NET Visual Studio 2019 form that will ask for a text file and output a list of names in a TextBox I have called TextBox4 so I don't have to create any files (or maybe create a text file, copy it to TextBox4, then delete it?). The names from a text file are between "Customer_Name" and "Customer_ID". The file doesn't seem to have any rhyme or reason other than those 2 identifiers, so splitting it up efficiently has been tough. There are generally between 100 and 1000 entries per file if that is relevant.
Sample (mock) data:
"Customer_name":"JOHN DOE","Customer_id":"9251954","Customer_team_id":"HOST","Customer_position_id":"MGR","Customer_short_name":"Joey","Customer_eligibility":"LT5","Customer_page_url":"google.com","Customer_alt_id":"M7","Customer_name":"JANE DOE","Customer_id":"8734817","Customer_team_id":"HOST","Customer_position_id":"TECH","Customer_name":"JOSEPH DOE","Customer_id":"8675307",
I would want to show this in the text box:
JOHN DOE
JANE DOE
JOSEPH DOE
CodePudding user response:
Take a look at this regular expression pattern:
(?:")(?<key>\w )(?:":")(?<value>((\w|\s|\.)) )(?:",)
This does several things:
(?:")- create non-capturing group to match the open quotation mark(?<key>\w )- create a named group to match the key (e.g. Customer_name)(?:":")- create a non-capturing group to match the close quotation mark, colon, and open quotation mark(?<value>((\w|\s|\.)) )- create a named group to match the value (e.g. John Doe)(?:",)- create a non-capturing group to match the close quotation mark
With this, you can loop over the matches and the matches' groups to get just the customer names:
' declare the pattern and input (escaping quotation marks) as well as a collection to store just the customer_name values
Dim pattern As String = "(?:"")(?<key>\w )(?:"":"")(?<value>((\w|\s|\.)) )(?:"",)"
Dim input As String = """Customer_name"":""JOHN DOE"",""Customer_id"":""9251954"",""Customer_team_id"":""HOST"",""Customer_position_id"":""MGR"",""Customer_short_name"":""Joey"",""Customer_eligibility"":""LT5"",""Customer_page_url"":""google.com"",""Customer_alt_id"":""M7"",""Customer_name"":""JANE DOE"",""Customer_id"":""8734817"",""Customer_team_id"":""HOST"",""Customer_position_id"":""TECH"",""Customer_name"":""JOSEPH DOE"",""Customer_id"":""8675307"""
Dim matches As MatchCollection = Regex.Matches(input, pattern)
Dim names As New List(Of String)()
' loop over each match
For Each match As Match In matches
' loop over each group in the match
For index As Integer = 0 To match.Groups.Count - 1
Dim group As Group = match.Groups.Item(index)
' only do something if we're on the "key" group, the "key" group's value is Customer_name, and there's at least one more group left
If (group.Name = "key" AndAlso group.Value = "Customer_name" AndAlso index < match.Groups.Count - 1)
' only do something if the next group is the "value" group
Dim valueGroup As Group = match.Groups.Item(index 1)
If (valueGroup.Name = "value") Then
' add the key's value
names.Add(valueGroup.Value)
Exit For
End If
End If
Next
Next
' set the TextBox's lines
TextBox4.Lines = names.ToArray()
Fiddle: https://dotnetfiddle.net/Zja46U
Edit - keep in mind that because we're using named groups, this code can be expanded to get any key/value pair now. It is just for the sake of this example that I'm getting only the Customer_name key/value pair.
