1

I'm trying to extract the status of 4 plug sockets via the html source code of the device, which is returned by a http.request.

The values i needed are recorded against the <script> var sockstates = e.g [1,0,1,0] ` but it can be anything 1/0 for each below.

I've created the following script as an example to try try and work it through, but I'm not able to extract the 4 values that are reported between the square brackets [1,1,1,1]

Any help on how i can focus in on that section and extract the values, would be appreciated.

local responseBody = [[ Example source code returned via http.request - <title>EnerGenie LAN Power Manager</title>

<meta http-equiv="content-type" content="text/html; charset=utf-8" /><link rel="stylesheet" type="text/css" href="s.css" />

<script>var sockstates = [1,1,1,1];var mac= "C58A6B4AACE2";var ipid = 0;var serv = 0;var trycon = 0;var active = 0;var actbtn = 1;var warn = 0;var timer= null;var period = 3000;
</script>]]

local socket1, socket2, socket3, socket4 = responseBody:match("(.-)<script>var sockstates = [(%d),(%d),(%d),(%d)];var mac=")

print(socket1, socket2, socket3, socket4)

1 Answer 1

2

[ and ] are magic characters used to define a character class. If you want to match them literally you need to escape them using %.

responseBody:match("(.-)<script>var sockstates = %[(%d),(%d),(%d),(%d)%];var mac=")

delivers the expectec result. Just notice that you have 5 captures. The 4 values in square brackets are captures 2-5. You print 1-4.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @piglet, sorry for any confusion I only need the 4 socket values, I had tried various things to see if I could get it to work and just posted my last attempt..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.