Reputation: 3
Need regex to pull out the access token from the below:
{
"access_token": "APWsWZi4CfK1cejU2Fn8u2xFtFKS_sDD3XlD6AKoydYTelIIadE5rarE6V2M_LVBD3ak_1WvaL0mlKYyCrSqubsbZCSidCLHB9kepR2ffw-O0Z8aMug4e7AYQ_gs_eWSygnFjbbOvCROp6mzvaBXsTEjn1J9Rtvt5yUzP1XKcHp4dQnO04MlwryZGO0Fuov4sMWpeml-8vB7o7H4hkQnSbR1yLuG_I6mmetKZqBMKibP_C3PndvnaFJzAVODDe3bGiubKELOu6jcSEOIxZKO38F_jXSDsrwIVbyrwYriD1menbh6hN7oFWdQzYc0U-5fxnAlfPm1yHTboAPxDqgIHKVOw4Wq-Ns7zAl9ZB16omRDP0yxNIG0hSQ7mT8xnf8tpsB7v3KdiHgDVbEe7P0mwKwpkQHUGp8-0B7P7iCaXWQmylLPh43yr68",
"token_type": "Bearer",
"expires_in": 300
}
Using ([A-Z]|-)\w+
pulls out my string but also Bearer
. I tried ([A-Z]|-)\w+(?!Bearer)
and it made no difference. Any other suggestions?
To be perfectly clear: the "access_token":
part can't be included. ONLY the token itself.
Upvotes: 0
Views: 27
Reputation: 805
If you need a regex that only matches the token and not anything else, the only regex I can think of that will work for you is
\w{36,}
where 36 (just an example) is the minimum number of characters in the access token. But this is very hacky. It is at least possible, if not likely, that another piece of information could get added that would also match that regex.
The reason this works is because in the JSON provided, the access token is by far the longest string of related characters. \w
is a shortcut for [a-zA-Z0-9_]
and \w{36,}
will match a string of at least 36 such characters. Because "
is not included, the string terminates at that point. In other words, as long as your number is larger than the longest word outside of the token, it will only pick up the token.
The real solution is to go with
"access_token": "(\w+)"
and reference the first capture group, if you have the ability to specify that.
Upvotes: 0