Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split complex string in VB.Net

I have a string of parameters from a Javascript function call

ITQPopup('100',255,'2932 NTYwNDUwMTA0MDYzMDM);3094 V0FZ','-1909432577',0,0)

As you can see, this is very poorly coded, there are 6 parameters being passed

'100'
255
'2932 NTYwNDUwMTA0MDYzMDM);3094 V0FZ'
'-1909432577'
0 
0

I would split the string by "," (comma) but I'm afraid there could be commas in the 3rd parameter. How would one go about splitting this string?

like image 714
Theveloper Avatar asked Jun 28 '26 19:06

Theveloper


2 Answers

You can accomplish this easily with RegEx. For instance:

Dim input As String = "ITQPopup('100',255,'2932 NTYwNDUwMTA0MDYzMDM);3094 V0FZ','-1909432577',0,0)"
Dim pattern As String = "ITQPopup\('(.*?)',(.*?),'(.*?)','(.*?)',(.*?),(.*?)\)"
Dim m As Match = Regex.Match(input, pattern)
If m.Success Then
    Dim param1 As String = m.Groups(1).Value
    Dim param2 As String = m.Groups(2).Value
    Dim param3 As String = m.Groups(3).Value
    Dim param4 As String = m.Groups(4).Value
    Dim param5 As String = m.Groups(5).Value
    Dim param6 As String = m.Groups(6).Value
End If

You could further improve the pattern, if necessary, to allow white-space between parameters, etc. But that's the simplest working example.

like image 164
Steven Doggart Avatar answered Jul 01 '26 10:07

Steven Doggart


Strip the content of the brackets, wrap in the square brackets and use JSON library to parse it as array.

like image 41
Karel Frajták Avatar answered Jul 01 '26 09:07

Karel Frajták