-2

I call an API in Visual Basic and it returns me this:

"access_token": "21652337f6733aec846eecd28bc421642a040ed68fd288bb2e1b96a3fa3",
"token_type": "bearer",
"expires_in": 7776000,
"created_at": 1471158248

I can save this response to a variable, but how do I filter out everything but the Access Token and then save that to a variable?

Thanks.

dpskipper
  • 5
  • 4

1 Answers1

1
Public Function GetAccessToken(input As String) As String
    Dim result = System.Text.RegularExpressions.Regex.Match(input, """access_token"":\s*""([^""]*)""")
    If result.Success Then
        Return result.Groups(1).Value
    End If
    Return ""
End Function
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • Thanks that worked but its not complete. The output I get from your code gives me the whole access_token line. I just want the actual random string minus quotations. So my output would look like: 21652337f6733aec846eecd28bc421642a040ed68fd288bb2e1b96a3fa3 ...No more no less. Thanks for the help. – dpskipper Aug 14 '16 at 11:44
  • Odd, are you sure you copied it exactly? There's only 1 capture group defined so groups(0) would give the whole line, groups(1) should give your value. – FloatingKiwi Aug 14 '16 at 12:48
  • Well, no. I didn't quite copy it exactly as your Public Function would not work inside of my sub, so i had to Frankenstein it with my very limited knowledge of VB. The line I did use was this one: Dim result = System.Text.RegularExpressions.Regex.Match(input, """access_token"":\s*""([^""]*)""") and when I printed the output of result.value I get the whole access token line. I did manage to work around this in the most convoluted method possible, but it does work. If you could make this one line only include the access token minus quotations I would be very happy. – dpskipper Aug 14 '16 at 23:35
  • Instead of printing `result.Value` print `result.Groups(1).Value`. However trying to fit things onto one line is generally a bad practice. – FloatingKiwi Aug 14 '16 at 23:48