Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua string.match() (Corona SDK)

Im trying to pull the auth code out of a server response like:

GET /?state=authenticated&code=U946s9lHouBGWy8o45bXSRSXGzTqd0Ys HTTP/1.1

I am using the lua/Corona;

string.match(request, "GET /?state=authenticated&code=([%w--_/.=?]+)")

I am getting a nil response and have no idea what Ive got wrong...anyone know or have a better idea?

wkr,

-sean

like image 613
user2467301 Avatar asked Sep 25 '26 08:09

user2467301


2 Answers

The character ? on its own, acts as a pattern modifier. This is why you get nil result. Use a % to escape this.

str = "GET /?state=authenticated&code=U946s9lHouBGWy8o45bXSRSXGzTqd0Ys HTTP/1.1"

print( str:match("GET /%?state=(%w+)&code=(%w+)") )

Here is working output: https://eval.in/33065


EDIT

Here is another example for the same, without escaping the ? character. This is just to elaborate my point. :)

like image 68
hjpotter92 Avatar answered Sep 26 '26 22:09

hjpotter92


Try to use this:

string.match(request, "GET /%?state=authenticated&code=([^ ]+)")
like image 24
Casimir et Hippolyte Avatar answered Sep 26 '26 23:09

Casimir et Hippolyte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!