Search code examples
stringluacoronasdklua-patterns

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


Solution

  • 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. :)